Re: [RFC] Nullsafe calls

From: Date: Tue, 09 Dec 2014 23:18:13 +0000
Subject: Re: [RFC] Nullsafe calls
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message

> On 9 Dec 2014, at 23:07, Josh Watzman <[email protected]> wrote:
> 
> Hey internals! A useful feature that Hack picked up in the last few months are "nullsafe
> calls", a way of propagating failure forward in a series of chained method calls to the end of
> the whole computation, getting rid of a lot of the boilerplate in the middle. I think the feature
> would be a good one for PHP as well, so I'm submitting this RFC to add it -- you can see the
> RFC itself for a full discussion of the motivation for the feature, as well as the feature itself:
> 
> https://wiki.php.net/rfc/nullsafe_calls

Hi!

In a way, this would complement the ?? null coalescing operator we’re adding. :)

Two thoughts, though:

1) Wouldn’t it be useful to also have this for properties? If you’re going down a long list of
property accesses, $foo?->bar?->qux is probably nicer than if ($foo !== NULL &&
$foo->bar !== NULL) { $foo->bar->qux; … } - on the other hand, the ?? operator partly
covers this and even handles arrays, as you can do
$foo->bar[‘elePHPant']->qux[‘boop’] ?? NULL already. We also couldn’t add
something like ?-> for arrays, as the ?[] syntax is ambiguous (looks like ternary with short
array syntax).

2) It’d probably be better if you made a language specification patch before, not after, the RFC
is accepted. Having to specify the syntax and semantics formally can make what the operator does
clearer and help to spot issues. Plus, going forward, the language specification should not be an
afterthought.

Otherwise, though, I’m in favour of this change.

Thanks!

--
Andrea Faulds
http://ajf.me/






Thread (28 messages)