Re: [RFC] anti-coalescing-operator
| From: | Andreas Hennings | Date: | Fri, 25 Oct 2019 10:27:06 +0000 |
| Subject: | Re: [RFC] anti-coalescing-operator | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107695@lists.php.net to get a copy of this message | ||
On Fri, 25 Oct 2019 at 12:21, Rowan Tommins <rowan.collins@gmail.com> wrote:
>
> On Thu, 24 Oct 2019 at 22:20, Kosit Supanyo <webdevxp.com@gmail.com> wrote:
>
> > But I think this functionality should be of something like pipeline
> > operator:
> >
> > // send tmp variable to the next expression unconditionally
> > $ret = $_SERVER['fname'] |> $user->setName($$);
> > // send tmp variable to the next expression only when $_SERVER['fname'] is
> > set.
> > $ret = $_SERVER['fname'] ?|> $user->setName($$);
> >
> > Also the syntax like above will be consistent with the proposed
> > safe-navigation operators.
> >
>
>
> I really like this idea, and it actually makes the pipeline operator itself
> feel more useful, too.
>
> Imagine this chain, where not only might the variable not be set, but it
> might not match a user, or the user might have no name:
>
> $upperCaseUserName = $_GET['user_id'] ?|> User::getById($$) ?-> getName()
> ?|> strtoupper($$);
>
> As well as not needing to repeat the expression each time, as you would
> with an "anti-coalesce", "null-safe chain" feels a clearer reading of the
> intent here than "if not unset".
I like this.
It is a bit like a temporary local variable.
One limitation is that you only have one such pipe value at a time.
E.g. it would not cover this:
return isset($a, $b) ? foo($a, $b) : null;
or any nested parameters like foo(bar(baz())) where any intermediate
value could be null.