Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51880 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52678 invoked from network); 14 Apr 2011 00:24:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 00:24:56 -0000 Authentication-Results: pb1.pair.com smtp.mail=mail_ben_schmidt@yahoo.com.au; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mail_ben_schmidt@yahoo.com.au; sender-id=unknown; domainkeys=good Received-SPF: error (pb1.pair.com: domain yahoo.com.au from 98.139.52.221 cause and error) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: mail_ben_schmidt@yahoo.com.au X-Host-Fingerprint: 98.139.52.221 nm24.bullet.mail.ac4.yahoo.com Received: from [98.139.52.221] ([98.139.52.221:31976] helo=nm24.bullet.mail.ac4.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5A/D3-44901-6DE36AD4 for ; Wed, 13 Apr 2011 20:24:55 -0400 Received: from [98.139.52.190] by nm24.bullet.mail.ac4.yahoo.com with NNFMP; 14 Apr 2011 00:24:52 -0000 Received: from [98.138.90.50] by tm3.bullet.mail.ac4.yahoo.com with NNFMP; 14 Apr 2011 00:24:52 -0000 Received: from [98.138.89.195] by tm3.bullet.mail.ne1.yahoo.com with NNFMP; 14 Apr 2011 00:24:52 -0000 Received: from [127.0.0.1] by omp1053.mail.ne1.yahoo.com with NNFMP; 14 Apr 2011 00:24:52 -0000 X-Yahoo-Newman-Id: 393907.2440.bm@omp1053.mail.ne1.yahoo.com Received: (qmail 63893 invoked from network); 14 Apr 2011 00:24:52 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.au; h=DKIM-Signature:Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=09y8VM9T6y0vU+VRUZYqE2xGrffzYw1fqyoUhZdowXKJ5djv2+pKTpWaXm5ly7P2A5x30RJYToA3bYqrKsA1909c2n20Xw0nsa9i05E3SEYiQy2C1eAjVr+kjaBwwrNwAf9fFemwZeHTM7cjgS+jJv+J3IBOE0xHeepCPI9j360= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s1024; t=1302740692; bh=f8peq70V2QLjX8Kz00vegMH51p4Wamjw4OXZXqebH8s=; h=Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=Mw8dTU9qkAjg2NnhUMSK7lpa4m7nyp9K0Qegj8hnz0oMxnMpyEml7YrYJJ8cPZzuCb57hsFQSpEOk0kGzF4G4roZR5xXUf/w2DxLkU75dP9elrrSoeS0Sx5lNxFKtWNIr48Wx1lAN30MqbOanJmaZ21rmqtjaEPlGff4KfSrPOI= Received: from thought.local (mail_ben_schmidt@124.168.69.175 with plain) by smtp133.mail.mud.yahoo.com with SMTP; 13 Apr 2011 17:24:51 -0700 PDT X-Yahoo-SMTP: enFMnPSswBAexaHyzgobwuUTrYOhZdJ0KRA2SjA- X-YMail-OSG: 7IigJ6IVM1nxEcJhY_Brgnr9szY51PgEH6mT.VSgcvPodvE Y0J8rGtOzQeeYiPpFVkWMNNoF7lLV_YMKt3KOi0K5PrKd4tZv7L9hHCEqXY8 gd4ASGWd90qcMko35sHyqx4Qm8kzkid.t40noPqAds2DW1dy.uzgvjr91T6X T2AK5n76KVkSEY12clSSxMRh.ny2AT5nNgeAsL2_.pQpPeW.LEHQZlEToWoj uAOEGxjVb5M_Z8bu54v7dpv7B12.pQU_qwjaiBo4k3ZYLN1pjDHbrKjpABQ9 wlnMjnp3hwqM2q3lt.RfRfv3qJ_dENdK_6RZsu8utwa83szuXFgni1GthfZ4 DNXWTH_ZaIG0UKmreumJDDlvRwg-- X-Yahoo-Newman-Property: ymail-3 Message-ID: <4DA63ED8.4080402@yahoo.com.au> Date: Thu, 14 Apr 2011 10:24:56 +1000 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: Ole Markus With , "internals@lists.php.net" References: <4D950434.3060704@yahoo.com.au> <4D9E0543.1080600@lerdorf.com> <69.82.36433.EC33E9D4@pb1.pair.com> <4D9E34C4.5000406@lerdorf.com> <4D9E429B.20503@sugarcrm.com> <4D9E96B6.6060401@lerdorf.com> <718216446.20110408143441@cypressintegrated.com> <4DA0E71C.9030008@gmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: mail_ben_schmidt@yahoo.com.au (Ben Schmidt) > I think these shortcuts could be really useful for array elements, but > for other variables I am really sceptical and I think they would do > more harm than good. Generally I do not really see any reason why a > variable should not be 'instanciated' before use. > > So > +1 if this shortcut is implemented to only work for array elements. > -1 for any other variable type. There are two issues here. 1. Suppression of notice. I agree, it is best done only for array keys. It's not hard to initialise a variable with $var=null at the beginning of a code block to avoid such a notice, and that is the appropriate way to do it for variables. 2. Offering a shortcut for the common idiom isset($x) ? $x : $y in line with the DRY design principle. A notice would never be emitted here in any case. The problem is that this idiom is still in wide use despite the shortcut ternary operator already provided, because an isset() check is different to a boolean cast. Some thoughts: - Changing the existing behaviour of the ternary or shortcut ternary operator is a bad idea. Good arguments have been offered against it. - The actual intent of 2. is probably $x!==null ? $x : $y i.e. it's not about suppressing notices at all, but about offering a default value, and the idiom quite probably only uses isset() because it predated null in the language. - If we view 2. in this way, the two problems are independent, and it seems to me it would be best to solve them independently, rather than with a single operator. So, I suggest: 1. An array lookup mechanism that suppresses the notice for undefined keys. It would work the same as regular array index lookups except that the notice for undefined keys (and only for undefined keys) would not be generated (it would not just be hidden, but would never be even generated). 2. A shortcut for $x!==null ? $x : $y. This allows people to opt out of either feature if they don't want it, and doesn't encourage poor coding by suppressing warnings for undefined variables. Arguing about syntax is then a second step. However, my views on this are: 1. Since we have @ meaning error-suppression already, why not reuse that symbol, in a location that is unambiguous? $array@[$key] would achieve this, right? It is localised to a single lookup, so can be as precise as the coder wants. E.g. one could do $array@["maybe-missing"]["expected"]@["maybe-missing"] and there's no ambiguity about what is meant. Nesting is likewise unambiguous, so in $array@[$lookups["key"]] if "key" does not exist, we still get a notice. 2. We just need a single symbol, probably with multiple characters. The key thing omitted by the shortcut is !==null so something that evokes that would be a good choice. Two possibilities are using something involving ? as in the ternary operator, or | as in the logical short-circuiting or operator. E.g. we could have $x ??: $y or $x ||| $y. However, both these somewhat imply either boolean tests or boolean results, which would be best to avoid. This is not about error suppression, so using @ would be unwise. Perhaps the best character to use is $ because $ somewhat represents a variable. So perhaps a good syntax would be $x $: $y. The $ somewhat evokes to me 'use the variable if you can (it is not null)' and the : somewhat evokes 'otherwise', so I think it captures the meaning of the operator. In combination (which some people probably want), this looks like $array@["key"] $: "default" It's a bit different to what we're used to seeing, but the syntax is (1) sensible and evocative of the right interpretation, and (2) not misleading. I'd rather see something I'm unsure about and drives me back to the docs than something that misleads me. Ben.