Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77300 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13583 invoked from network); 17 Sep 2014 17:23:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2014 17:23:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.48 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.48 mail-wg0-f48.google.com Received: from [74.125.82.48] ([74.125.82.48:62758] helo=mail-wg0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/59-57031-D73C9145 for ; Wed, 17 Sep 2014 13:23:10 -0400 Received: by mail-wg0-f48.google.com with SMTP id m15so1741009wgh.19 for ; Wed, 17 Sep 2014 10:23:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=oMsJkTLsImnOUixmDuUbTorMocQWd/lDYaAXuAO9VSg=; b=SHppCZJxnHAT+xFsHoi2s0anxMg0pdVenacr9GL1hlXEWhQm/DCQ6t/Z8scEFGvVVE am2Cr7BuhuS44EnBKOP4BU45hG4TmqNccZ+APZswdwkt66vfpvpIsTLW6cdhtaKSw/hF 3DmCzsetD3YWv45JhxIgmD1g4w3phk7StnSbcAixnaclwrMRUiqmsU/1DjPpnfrcVINH hc3blP75U96cbf+q91FMS3T5JrBdCfIX0d1v1xd1TxYsqw9jj6SZIC1GTBriKuW6y0z+ KSQXDCXqJtdwYtWyef70TX5fXXeyuNogYGgVytqWPAU0soRRv5c7XK2lOI3jDmYJ3i5k S1Mg== X-Received: by 10.180.98.131 with SMTP id ei3mr41893153wib.56.1410974587013; Wed, 17 Sep 2014 10:23:07 -0700 (PDT) Received: from [192.168.0.177] ([62.189.198.114]) by mx.google.com with ESMTPSA id ba3sm6335037wib.10.2014.09.17.10.23.05 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 17 Sep 2014 10:23:05 -0700 (PDT) Message-ID: <5419C376.2090007@gmail.com> Date: Wed, 17 Sep 2014 18:23:02 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Implicit isset() in Shorthand Ternary Operator From: rowan.collins@gmail.com (Rowan Collins) Matthew Fonda wrote (on 17/09/2014): > Hi Andrea, > > This is great -- thanks to you and Nikita for the work here. > > Syntax wise, I would prefer a function-like syntax, e.g. coalesce($a, $b, > 'c') or ifsetor() instead of $a ?? $b ?? 'c'. I find this more readable, > and it avoids any possible confusion about precedence within the > expressions. Either way, still +1 for this feature. Funnily enough, the standard library at my workplace has long included a coalesce() function as below, which is indeed very handy; there is also a coalesce_empty(), which calls empty($arg) instead of is_null($arg). Both pre-date the shorthand ?: operator, which could replace coalesce_empty(); the proposed ?? could replace coalesce() As mentioned in the PHPDoc, the downside of a function is that it can't short-cut evaluation if the first argument passes, whereas an operator can. It would be possible to work around this by defining a special "language construct" rather than a real function, but that's probably more confusing than it's worth. The special case for $first is a performance optimisation because func_get_args() has rather a lot of overhead; I'm guessing 5.6's variadic signatures ($args...) are less painful (can anyone confirm?) /** * coalesce * PHP analogue of the SQL Coalesce() function * @param an arbitrary number of arguments * @return the first argument which is not NULL; If all arguments are NULL, will return NULL * * coalesce( $a, $b ) is equivalent to ($a === NULL ? $a : $b) * but coalesce( hard_work(), 'default' ) only has to do hard_work() once * [OTOH, coalesce( $a, hard_work() ) has to do hard_work() even if the result gets thrown away] */ function coalesce($first) { if (!is_null($first)) { return $first; } foreach ( func_get_args() as $arg ) { if ( ! is_null($arg) ) { return $arg; } } } -- Rowan Collins [IMSoP]