Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

131–140 of 282 posts

Re: PHP 8.5 adds pipe operator

#131

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,…

> The -> is a leftover from an older programming language that I'd rather have replaced by a ., but not at the cost of breaking existing code (which it surely would).

Isn't it because . was already used for string concatenation in PHP. I mean the -> syntax wasn't invented by PHP but it didn't just inherit it without thought either.

Re: PHP 8.5 adds pipe operator

#132
post #62
post #35

Earlier quoted context omitted.

Other languages have all sorts of oversized arrows, like ==> and >>>. -> in PHP and C++ looks clean by comparison. I'll never forgive them for the brain fart they made of the namespace separator, though.

What would the alternative for a namespace separator be? The backslashes work well with PSR-4 to give a logical visual of the expected directory structure.

Since PHP takes a lot of syntax from C-like languages, the C++ namespace separator :: would be the obvious choice. Not sure if this conflicted with something in PHP though.

Re: PHP 8.5 adds pipe operator

#133
post #55
post #35

Earlier quoted context omitted.

Other languages have all sorts of oversized arrows, like ==> and >>>. -> in PHP and C++ looks clean by comparison. I'll never forgive them for the brain fart they made of the namespace separator, though.

> I'll never forgive them for the brain fart they made of the namespace separator, though. You mean the backslash? What's wrong with that?

To someone not already familiar with PHP it looks like you are trying to escape something.

Re: PHP 8.5 adds pipe operator

#136
post #55
post #35

Earlier quoted context omitted.

Other languages have all sorts of oversized arrows, like ==> and >>>. -> in PHP and C++ looks clean by comparison. I'll never forgive them for the brain fart they made of the namespace separator, though.

> I'll never forgive them for the brain fart they made of the namespace separator, though. You mean the backslash? What's wrong with that?

The backslash is universally reserved as an escape character.

It was decided almost 20 years ago so I'm totally used to it and there's no point arguing about it anymore. But the decision to reuse the backslash as a namespace separator still causes inconvenience from time to time. For example, when you write PSR-4 configuration in composer.json, all the backslashes need to be doubled, including (and especially!) the trailing backslash.

Re: PHP 8.5 adds pipe operator

#137
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.

It's easier to write, copy paste, compose and comment

Re: PHP 8.5 adds pipe operator

#138
The first typed programming language where I've seen pipe operator |> in action was in F#. You can write something like:

  sum 1 2
  |> multiply 3

and it works because |> pushes the output of the left expression as the last parameter into the right-hand function. multiply has to be defined as:

  let multiply b c = b \* c

so that b becomes 3, and c receives the result of sum 1 2.

RHS can also be a lambda too:

  sum 1 2 |> (fun x -> multiply 3 x)

|> is not a syntactic sugar but is actually defined in the standard library as:

  let (|>) x f = f x

For function composition, F# provides >> (forward composition) and (backward composition), defined respectively as:

  let (>>) f g x = g (f x)
  let (
We can use them to build reusable composed functions:

  let add1 x = x + 1
  let multiply2 x = x \* 2
  let composed = add1 >> multiply2 

F# is a beautiful language. Sad that M$ stopped investing into this language long back and there's not much interest in (typed) functional programming languages in general.

Re: PHP 8.5 adds pipe operator

#139
post #25
post #4

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) => ar…

PHP has traits, just invent that API, put it in a trait and add it to your data classes.

How would that work if a library supplies a function that takes a string and returns an array? I can’t make it use my array class.

Re: PHP 8.5 adds pipe operator

#140
Makes for a fun programming paradigm, similar to Java's streams-with-lambdas. Great for readability. Not too fond of the |> operator though, requires 4 different keypresses on my keyboard layout, not terribly ergonomic. But I understand that options were limited and it is sort of clear.
Post reply on HN