Re: [RFC] Pipe Operator (again)
| From: | Larry Garfield | Date: | Wed, 26 Feb 2025 06:26:27 +0000 |
| Subject: | Re: [RFC] Pipe Operator (again) | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126510@lists.php.net to get a copy of this message | ||
On Mon, Feb 10, 2025, at 4:04 AM, Tim Düsterhus wrote:
> Hi
>
> Am 2025-02-07 05:57, schrieb Larry Garfield:
>> https://wiki.php.net/rfc/pipe-operator-v3
>
> After also having taken a look at the implementation and then the
> updated “Precedence” section, I'd like to argue in favor of moving
>
|>
> to have a higher precedence than the comparison operators (i.e. between
> string concatenation and <). This would mean that
> |> has higher
> precedence than ??, but looking at the following examples, that
>
> appears to be the more useful default anyways.
>
> I'm rather interested in handling a null pipe result:
>
> $user = $request->get('id')
> |> $database->fetchUser(...)
> ?? new GuestUser();
>
> Than handling a null callback (using the RFC example, because I can't
> even think of a real-world use-case):
>
> $res1 = 5
> |> $null_func ?? defaultFunc(...);
>
> To give some more examples of what would be possible without parentheses
> then:
>
> $containsNotOnlyZero = $someString
> |> fn ($str) => str_replace('0', '', $str)
> |> strlen(...)
> > 0;
>
> Which is not particularly pretty, but appears to be more useful than
> either passing a boolean into a single-argument function or piping into
> a boolean (which would error).
>
> Best regards
> Tim Düsterhus
I have updated the patch and RFC accordingly. I think you're right, it does make a bit more
sense this way.
--Larry Garfield