feat: add pipe assignment operator (|>=)#22633
Conversation
2d61f14 to
b933907
Compare
NickSdot
left a comment
There was a problem hiding this comment.
Got this review myself so it caught my eye.
b933907 to
46028af
Compare
You must do it by yourself after karma points will be granted to you. FYI: your email landed in my Gmail mailbox to spam. https://news-web.php.net/php.internals/131804 |
|
No idea, honestly. Google fights with ProtonMail around this topic, and it's quite long concern. You can check 4 years old topic here: https://www.reddit.com/r/ProtonMail/comments/v26rhi/proton_mail_emails_going_to_spam/ |
Adds the `|>=` compound assignment operator for the pipe operator, allowing `$x |>= callable` to be used as shorthand for `$x = $x |> callable`. Supports pipe chains on the RHS: `$x |>= fn1(...) |> fn2(...) |> fn3(...)` is equivalent to `$x = $x |> fn1(...) |> fn2(...) |> fn3(...)`. Implementation modeled after `??=` (ZEND_AST_ASSIGN_COALESCE), with a dedicated AST node (ZEND_AST_ASSIGN_PIPE) and compiler function that uses memoization to avoid double-evaluating complex LHS expressions. Extracts shared callable dispatch logic into zend_pipe_build_fcall() helper used by both `|>` and `|>=` compilation. All variable target types supported: simple vars, array dims, object properties, static properties.
46028af to
4253972
Compare
| $str = " hello "; | ||
| $str |>= trim(...); | ||
| var_dump($str); | ||
|
|
||
| $data = ["a" => 1, "b" => 2, "c" => 3]; | ||
| $data |>= array_values(...); | ||
| var_dump($data); |
There was a problem hiding this comment.
Two of the three cases can be removed. There is no reason why the operator would work with array_unique, but not with any of the other functions. I'd probably keep the trim() one (or make it a strtoupper() one), as it's the simplest of them. You don't want to test that array_unique() works correctly, you want to test the behavior of the assign-pipe.
| $x = [3, 1, 4, 1, 5]; | ||
| $x |>= array_unique(...) |> array_values(...); | ||
| var_dump($x); | ||
|
|
||
| $s = " Hello World "; | ||
| $s |>= trim(...) |> strtolower(...) |> str_split(...); | ||
| var_dump($s); |
There was a problem hiding this comment.
And similarly, if it works for 2, it will also work for 3 - and vice versa. The other two have some value, because they test the behavior of non-FCC Closures.
| <?php | ||
|
|
||
| $x = "hello"; | ||
| $x |>= strtoupper(...); |
There was a problem hiding this comment.
Similarly, decide on a single function (consistent with 001), since the goal of the test is to test the variable types. This makes it much easier to verify the correctness, because it's clear what the expected output is.
| return $v; | ||
| } | ||
|
|
||
| $arr = [["hello"]]; |
There was a problem hiding this comment.
This input can be simplified now (and the expectation adjusted). Staying in line with the “consistency” argument and retracting the initial review somewhat: Probably also make this a trim() or strtoupper() test then.
| var_dump($c, $d); | ||
|
|
||
| $e = 4; | ||
| $f = ($e |>= triple(...)) |> add_five(...) |> to_string(...); |
There was a problem hiding this comment.
Looking at this, we should probably also test:
function x($bar) {
var_dump($bar);
return function ($foo) { return $foo + 1; };
}
$bar = 0;
$foo = 1;
$foo |>= $bar |>= x(...);
This should result in $bar being a Closure and $foo being 1 if I'm not mistaken.
Perhaps you can think of more interesting combinations for precedence with various placement of parentheses.
There was a problem hiding this comment.
This is likely redundant with Zend/tests/pipe_operator/assign_pipe_004.phpt and Zend/tests/pipe_operator/assign_pipe_003.phpt. There is no reason why pipe-chains on properties would behave any differently than pipe chains on regular variables.
There was a problem hiding this comment.
This one should also test the behavior when parentheses or other operators are added, e.g. $x |>= ($callback ?? fallback(...)).
Hello!
(aka, the weightlifter operator)
RFC: tbd (as soon as I get them wiki karma, username
cdwhite3)Thanks!