I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chainable.
I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator
colecut · 5m ago
PHP string / array functions are consistent.
string functions use (haystack, needle) and
array functions use (needle, haystack)
because that's the way the underlying C libraries also worked
noduerme · 7m ago
Agree, the ... syntax feels confusing when each fn($x) in the example uses $x as the name of its argument.
My initial instinct would be to write like this:
`$result = $arr
|> fn($arr) => array_column($arr, 'tags') // Gets an array of arrays
|> fn($cols) => array_merge(...$cols)`
Which makes me wonder how this handles scope. I'd imagine the interior of some chained function can't reference the input $arr, right? Does it allow pass by reference?
It’s really not needed, syntax sugar. With dots you do almost the same. Php doesn’t have chaining. Adding more and more complexity doesn’t make a language better.
bapak · 1h ago
Nothing is really needed, C89 was good enough.
Dots are not the same, nobody wants to use chaining like underscore/lodash allowed because it makes dead code elimination impossible.
It looks like chaining, but with possibility of adding custom functions?
bapak · 42m ago
It's chaining without having to vary the return of each function. In JS you cannot call 3.myMethod(), but you could with 3 |> myMethod
cyco130 · 21m ago
It requires parentheses `(3).myMethod()` but you can by monkey patching the Number prototype. Very bad idea, but you absolutely can.
te_chris · 20m ago
Dots call functions on objects, pipe passes arguments to functions. Totally missing the point.
lacasito25 · 6m ago
in typescript we can do this
let res
res = op1()
res = op2(res.op1)
res = op3(res.op2)
type inference works great, and it is very easy to debug and refactor. In my opinion even more than piping results.
Javascript has enough features.
ossusermivami · 1m ago
i wish python had something liek that to be honest
sandreas · 1h ago
While I appreciate the effort and like the approach in general, in this use case I really would prefer extensions / extension functions (like in Kotlin[1]) or an IEnumerable / iterator approach (like in C#).
$arr = [
new Widget(tags: ['a', 'b', 'c']),
new Widget(tags: ['c', 'd', 'e']),
new Widget(tags: ['x', 'y', 'a']),
];
$result = $arr
|> fn($x) => array_column($x, 'tags') // Gets an array of arrays
|> fn($x) => array_merge(...$x) // Flatten into one big array
|> array_unique(...) // Remove duplicates
|> array_values(...) // Reindex the array.
;
PHP has traits, just invent that API, put it in a trait and add it to your data classes.
troupo · 1h ago
The advantage is that pipes don't care about the type of the return value.
Let's say you add a reduce in the middle of that chain. With extension methods that would be the last one you call in the chain. With pipes you'd just pipe the result into the next function
phplovesong · 36m ago
The stdlib is so inconsistent this will be a nightmare.
Optionally with a better language you know what order params as passed (array_map / array_filter), but in PHP is a coin coss.
This feels very bolted on and not suited for the stdlib at all.
PHP devs should instead FIRST focus on full unicode support (no, the mb_real_uppercase wont do), and only then focus on a new namespaced stdlib with better design.
habibur · 19m ago
I tried to emulate something similar with PHP at one point. But the problem with PHP was parameter order. Especially in functions like array_key_exists() the array element is the 2nd parameter, while pipe operator expects the object to work on be the 1st parameter, the array in these cases.
I believe they have solved this problem by now. Though no idea how.
cess11 · 2m ago
"A major limitation of the pipe operator is that all the callables in the chain must accept only one required parameter.
For built-in functions, if the function does not accept any parameters, it cannot be used in a chain. For user-land PHP functions, passing a parameter to a function that does not accept any parameters does not cause an error, and it is silently ignored.
With the pipe operator, the return value of the previous expression or the callable is always passed as the first parameter to the next callable. It is not possible to change the position of the parameter."
In the light of these limitations I would not call the Elixir implementation "slightly fancier".
I'm not so sure I'll be upgrading my local PHP version just for this but it's nice that they are adding it, I'm sure there is a lot of library code that would look much better if rewritten into this style.
librasteve · 1h ago
raku has had feed operators like this since its inception
Which has the advantage of also offering a clean alternative to the fragmented stdlib.
lordofgibbons · 31m ago
Why doesn't PHP remove the horrid $ symbol for variables and the -> symbol for calling methods? I think those alone would do a lot more for its perception and adoption than adding the pipe operator.
phatskat · 9m ago
I actually don’t mind them, and I’ve been out of daily PHP work for a few years now. When I see people denote internal variables with _ or elements with $ in JS, it rubs me the wrong way, but in PHP the $ is kind of nice.
I also prefer the look of ->, it’s _cool_
JaggerJo · 24m ago
Thanks F#!
keyle · 1h ago
C'mon Dart! Follow up please. Go is a lost cause...
tayo42 · 41m ago
I feel like a kindergartener writing go. I wish another language got popular in the space go is used for.
avkrpatel · 37m ago
This will be game changer for AI.
edhelas · 24m ago
What?
keybored · 18m ago
Prompt: Mention how this will affect AI on every topic.
I really believe the thing PHP needs the most is a rework of string / array functions to make them more consistent and chain able. Now they are at least chainable.
I'm not a fan of the ... syntax though, especially when mixed in the same chain with the spread operator
string functions use (haystack, needle) and array functions use (needle, haystack)
because that's the way the underlying C libraries also worked
My initial instinct would be to write like this:
`$result = $arr
Which makes me wonder how this handles scope. I'd imagine the interior of some chained function can't reference the input $arr, right? Does it allow pass by reference?Dots are not the same, nobody wants to use chaining like underscore/lodash allowed because it makes dead code elimination impossible.
Keyword: almost. Pipes don't require you to have many different methods on every possible type: https://news.ycombinator.com/item?id=44794656
More like thenables / promises
let res res = op1() res = op2(res.op1) res = op3(res.op2)
type inference works great, and it is very easy to debug and refactor. In my opinion even more than piping results.
Javascript has enough features.
1: https://kotlinlang.org/docs/extensions.html#extension-functi...
Let's say you add a reduce in the middle of that chain. With extension methods that would be the last one you call in the chain. With pipes you'd just pipe the result into the next function
Optionally with a better language you know what order params as passed (array_map / array_filter), but in PHP is a coin coss.
This feels very bolted on and not suited for the stdlib at all.
PHP devs should instead FIRST focus on full unicode support (no, the mb_real_uppercase wont do), and only then focus on a new namespaced stdlib with better design.
I believe they have solved this problem by now. Though no idea how.
For built-in functions, if the function does not accept any parameters, it cannot be used in a chain. For user-land PHP functions, passing a parameter to a function that does not accept any parameters does not cause an error, and it is silently ignored.
With the pipe operator, the return value of the previous expression or the callable is always passed as the first parameter to the next callable. It is not possible to change the position of the parameter."
https://php.watch/versions/8.5/pipe-operator
In the light of these limitations I would not call the Elixir implementation "slightly fancier".
I'm not so sure I'll be upgrading my local PHP version just for this but it's nice that they are adding it, I'm sure there is a lot of library code that would look much better if rewritten into this style.
true it is syntax sugar, but often the pipe feed is quite useful to make chaining very obvious
https://docs.raku.org/language/operators#infix_==%3E
$myString.trim().replace("w", "h");
Which has the advantage of also offering a clean alternative to the fragmented stdlib.
I also prefer the look of ->, it’s _cool_