Skip to content

feat: add pipe assignment operator (|>=)#22633

Open
calebdw wants to merge 1 commit into
php:masterfrom
calebdw:calebdw/push-zkppokymsmqt
Open

feat: add pipe assignment operator (|>=)#22633
calebdw wants to merge 1 commit into
php:masterfrom
calebdw:calebdw/push-zkppokymsmqt

Conversation

@calebdw

@calebdw calebdw commented Jul 8, 2026

Copy link
Copy Markdown

Hello!

(aka, the weightlifter operator)

RFC: tbd (as soon as I get them wiki karma, username cdwhite3)

Thanks!

@NickSdot NickSdot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this review myself so it caught my eye.

Comment thread Zend/tests/pipe_operator/assign_pipe_009.phpt Outdated
Comment thread Zend/tests/pipe_operator/assign_pipe_014.phpt Outdated
@calebdw calebdw force-pushed the calebdw/push-zkppokymsmqt branch from b933907 to 46028af Compare July 8, 2026 11:59
@jorgsowa

jorgsowa commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

can someone create this page for me please? https://wiki.php.net/rfc/pipe_assignment_operator)

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

@calebdw calebdw requested a review from NickSdot July 8, 2026 14:52
@calebdw

calebdw commented Jul 8, 2026

Copy link
Copy Markdown
Author

FYI: your email landed in my Gmail mailbox to spam. https://news-web.php.net/php.internals/131804

☹️ that's not good---any idea why?

@jorgsowa

jorgsowa commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

that's not good---any idea why?

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/

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some remarks to the tests.

Comment thread Zend/tests/pipe_operator/assign_pipe_001.phpt Outdated
Comment thread Zend/tests/pipe_operator/assign_pipe_002.phpt Outdated
Comment thread Zend/tests/pipe_operator/assign_pipe_004.phpt
Comment thread Zend/tests/pipe_operator/assign_pipe_005.phpt Outdated
Comment thread Zend/tests/pipe_operator/assign_pipe_009.phpt Outdated
Comment thread Zend/zend_ast.c
Comment thread Zend/tests/pipe_operator/assign_pipe_017.phpt
Comment thread Zend/tests/pipe_operator/assign_pipe_014.phpt Outdated
Comment thread Zend/tests/pipe_operator/assign_pipe_010.phpt
Comment thread Zend/tests/pipe_operator/assign_pipe_006.phpt Outdated
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.
@calebdw calebdw force-pushed the calebdw/push-zkppokymsmqt branch from 46028af to 4253972 Compare July 8, 2026 21:04
Comment on lines +10 to +16
$str = " hello ";
$str |>= trim(...);
var_dump($str);

$data = ["a" => 1, "b" => 2, "c" => 3];
$data |>= array_values(...);
var_dump($data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +6 to +12
$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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should also test the behavior when parentheses or other operators are added, e.g. $x |>= ($callback ?? fallback(...)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants