AW: [PHP-DEV] [RFC] Nullsafe calls
| From: | Robert Stoll | Date: | Wed, 10 Dec 2014 20:30:04 +0000 |
| Subject: | AW: [PHP-DEV] [RFC] Nullsafe calls | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79547@lists.php.net to get a copy of this message | ||
> -----Ursprüngliche Nachricht-----
> Von: Josh Watzman [mailto:jwatzman@fb.com]
> Gesendet: Mittwoch, 10. Dezember 2014 19:43
> An: Robert Stoll
> Cc: PHP internals
> Betreff: Re: [PHP-DEV] [RFC] Nullsafe calls
>
> On Dec 10, 2014, at 8:17 AM, Robert Stoll <php@tutteli.ch> wrote:
>
> > First of all, I like the RFC, I think as well that it is a useful
> > feature for PHP and I already have it on my wish list
> > ;)
> >
> > Yet, either I misunderstand the section about short circuiting or I propose to change the
> > behaviour.
> >
> > "If $obj is null, when $obj?->foo(..) executes, the arguments will still be
> > evaluated."
> >
> > IMO that is wrong. It should not evaluate the arguments since the nullsafe operator
> > (sometimes called "safe
navigation"
> > operator in other languages - which is the better wording IMO, maybe
> > change it? But that is just a detail) is just syntactic sugar for making the call only if
> > $bj is not null. So the
following:
> >
> > [...]
> >
>
> Take a look at the "short circuit" section where I specifically address this. It was
> done to make it easier to reason
about in a
> side-effect-ful language like PHP. With this behavior, when you see
>
> $x?->f(g(), h())
>
> you don't have to worry about thinking whether g() and h() will actually be evaluated or
> not -- only the call to f()
itself,
> which is explicitly marked as nullsafe (i.e., might not happen). It also comes down to what you
> mean by "not making
the
> method call when $x is null" -- we in fact don't make the method call, but we still
> prepare everything as if we were
going to,
> and just decide not to do it at the last moment.
>
> I feel pretty strongly that this is the correct behavior. It may sound strange when talking
> about isolated examples
like this,
> but in the real-world code I've seen, it is the least confusing. (I'll see if I can
> dig up a better example instead of
just citing
> nebulous "real-world code" ;))
>
> Josh
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit:
> http://www.php.net/unsub.php
I stick with it, evaluating it does not make sense IMO. If I want to execute it in any case then I
would do something
like this currently
$g = g();
$h = h();
if($x !== null){
$x->foo($g, $h)->bar(baz());
}
And with the ?-> operator:
$g = g();
$h = h();
$x?->foo($g, $h)?->bar(baz());
Where I do not expect that baz() is called. You wrote it will be confusing for the user if the
arguments would not be
evaluated. I do not think so, telling them it is a short form for ($x !== null ? $x->foo() :
null) is very easy and
straight forward for the user to pick up. Therefore, please dig up a better example. Right now, I
think it is a good
idea in general to add a nullsafe operator to PHP but not only in the way mentioned.