-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pipe operator #5425
Pipe operator #5425
Conversation
Regarding your "comparison with other languages" in https://wiki.php.net/rfc/pipe-operator-v2, the Ruby equivalent is actually the
|
As there was no comment for two month and from the rfc I can't really see if and where's the discussion: |
As a tweaked example from the RFC, would you want this code in your codebase? $result = "Hello World"
|> 'htmlentities'
|> 'explode'
|> fn($x) => array_map('strtoupper', $x)
|> fn($x) => array_filter($x, fn($v) => $v != 'O'); It would be better if we could use actual symbols instead of strings, and if we could bind directly to a parameter. Syntax subject to change, but something like this where $result = "Hello World"
|> htmlentities(...)
|> explode(...)
|> array_map(strtoupper(...), $$)
|> array_filter($$, fn($v) => $v != 'O'); The feeling was that for An implementation of partial function application, which would create closures from things like Aside: I think it would be even better if we could just use the function names directly, but this would be a major compatibility change and would be much harder to convince internals to accept: $result = "Hello World"
|> htmlentities
|> explode
|> array_map(strtoupper, $$)
|> array_filter($$, fn($v) => $v != 'O'); Today a bare |
If I may hazard a comment, I would prefer:
How about
|
At present, there is some on again, off again work on partial function application. If that passes, it would largely resolve the pushback this got. At that point I intend to revive this RFC. Otherwise, "What Levi said." |
Hi @guss77, I like your ideas.
Yes, 100%.
Why not? Unfortunately there's no language support yet for passing functions around like this, but the following code samples look very clean to me:
|
@dandrei As Levi said above, just writing |
Thanks for the reply, @Crell, you're right about function names by themselves looking like constants to the engine.
|
@dandrei :
Because it looks ambiguous and confusing. Java uses "method references" like so: I think the grammar should simply mirror anonymous callables, just with more optional parts. An anonymous function looks like this:
Pipe operation can look like this:
|
I specifically do not want to include any special callable syntax for pipe; they should support whatever callable syntax PHP supports. As Levi notes above, we're hoping to get partial application into 8.1 which would fulfill the goal of a simpler callable syntax, which would also make pipe cleaner. Until that happens there's nothing else to discuss on this thread. |
| expr T_PIPE expr | ||
{ $$ = zend_ast_create(ZEND_AST_CALL, $3, zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1) ); $$->attr = ZEND_CALL_SYNTAX_PIPE; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikic Is there something we could/should do with CG(zend_lineno)
here? I don't know how the global is really used, so I don't know if this would have any positive impact, or if it should be elsewhere on the line (and I didn't try compiling this):
| expr T_PIPE { $<num>$ = CG(zend_lineno); } expr
{ $$ = zend_ast_create(ZEND_AST_CALL, $4, zend_ast_create_list(1, ZEND_AST_ARG_LIST, $1) ); $$->attr = ZEND_CALL_SYNTAX_PIPE; }
Was trying to think of how we can make it as helpful as possible for debuggers, profilers, etc to have accurate line numbers.
Closing, superseded, then declined. |
Pipe operator RFC.
At Levi Morison's recommendation, this is implemented purely in the parser layer. The new sigil desugars automatically to the equivalent "inside out" syntax, which means basically all error handling is already written automatically.