Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

101–110 of 282 posts

Re: PHP 8.5 adds pipe operator

#101
This looks neat. However since I read about Koka's dot selection [0], I keep thinking that this is an even neater syntax:

fun showit( s : string )

  s.encode(3).count.println
However, this is of course impossible to implement in most languages as the dot is already meaningful for something else.

[0] https://koka-lang.github.io/koka/doc/book.html#sec-dot

Re: PHP 8.5 adds pipe operator

#102
post #29

I like it. 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

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

So they are consistent because they are consistently inconsistent??

There isn't a good reason for PHP to have inherited C's issues here.

Re: PHP 8.5 adds pipe operator

#103
post #101

This looks neat. However since I read about Koka's dot selection [0], I keep thinking that this is an even neater syntax: fun showit( s : string ) s.encode(3).count.println However, this is of course impossible to implement in most languages as the dot is already meaningful for something else. [0] https://koka-lang.github.io/koka/doc/book.html#sec-dot

I think this is called uniform function call syntax.

Re: PHP 8.5 adds pipe operator

#105

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.

Same reason C doesn't introduce classes and C++ doesn't remove pointers: it's a) part of the core language and b) extremely inconsequential for any serious developer. I actually like the clarity these dollar signs add in a code base. Makes it easier to recognise (dynamic) functions, and makes it harder to accidentally shadow methods. Other languages will let you do `const Math = {}` and nuke the entire math library,…

I do also appreciate that php has an explicit string concat operator rather than overloading +. Though of course it could just use another symbol for that to get rid of -> if we're talking about time travel. As it stands, you can't really do $obj.method(), because method() could be a function returning a string as well, so it's ambiguous

Re: PHP 8.5 adds pipe operator

#106

I like it. 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

The syntax could be improved by allowing you to omit the (...) part entirely for single argument functions and using currying for functions that need additional arguments. So you would end up with something like:

  $result = $arr
      |> select_column('tags')         // Gets an array of arrays
      |> fn($x) => array_merge(...$x)  // Flatten into one big array
      |> array_unique                  // Remove duplicates
      |> array_value                   // Reindex the array.

Re: PHP 8.5 adds pipe operator

#107
post #45

I love the pipe operator - one of the things I dig about Elixir though many languages have it. It's so much easier to reason about: $result = $arr |> fn($x) => array_column($x, 'tags') |> fn($x) => array_merge(...$x) |> array_unique(...) |> array_values(...) VS array_values(array_unique(array_merge(...array_column($arr, 'tags'))));

I don't see how this is hard to reason about, assuming this is the resulting code when using variables: $tags = ...array_column($arr, 'tags'); $merged_tags = array_merge($tags); $unique_tags = array_unique($merged_tags); $tag_values = array_values($unique_tags); It also makes it easier to inspect the values after each step.

Introducing a new variable every single line adda a bunch of cognitive load compared to the pipe operator.

It's much easier skim with the pipe operator and it's more robust too (for example reordering is a pain with variables, it's easy to introduce errors).

Re: PHP 8.5 adds pipe operator

#108
post #95

Earlier quoted context omitted.

It is to interject the chained value at the right position in the function. They write that elixir has a slightly fancier version, it is likely around this, they mean (where elixir has first class support for arity > 1 functions)

But the example suggests that it can't interject the chained value at the right position; if that was the case, the example would've been written as `|> array_column('tags', ...)`.

yeah that sounds weird. defaulting to the first (or only) parameter would have made sense.

Re: PHP 8.5 adds pipe operator

#109
post #101

This looks neat. However since I read about Koka's dot selection [0], I keep thinking that this is an even neater syntax: fun showit( s : string ) s.encode(3).count.println However, this is of course impossible to implement in most languages as the dot is already meaningful for something else. [0] https://koka-lang.github.io/koka/doc/book.html#sec-dot

That syntax is very clean when it works. I think however the limitation of not being able to pipe arguments into 2nd, 3rd, ..., positions and keyword arguments, or variadic explosion like the syntax showcased in the article makes it less powerful.

Are there other syntax helpers in that language to overcome this?

Re: PHP 8.5 adds pipe operator

#110
post #2

Meanwhile the JS world has been waiting for 10 years for this proposal, which is still in stage 2 https://github.com/tc39/proposal-pipeline-operator/issues/23...

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.

When you say chaining, do you mean autoboxing primitives? PHP can definitely do things like `foo()->bar()?->baz()`, but you'd have to wrap an array/string yourself instead of the methods being pulled from a `prototype` to use it there.
Post reply on HN