Live data from Hacker News

PHP 8.5 adds pipe operator

thephp.foundation

171–180 of 282 posts

Re: PHP 8.5 adds pipe operator

#171
post #142

I had this argument in the PHP community when the feature was being discussed, but I think the syntax is much more complicated to read , requiring backtracking to understand. It might be easier to write. Imagine you're just scanning code you're unfamiliar with trying to identify the symbols. Make sense of inputs and outputs, and you come to something as follows. $result = $arr |> fn($x) => array_column($x, 'values')…

This is what a good IDE brings to the table, it'll show that $result is of type string.

The pipe operator (including T_BLING) was one of the few things I enjoyed when writing Hack at Meta.

Re: PHP 8.5 adds pipe operator

#172
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

It is called Uniform [Function] Call Syntax.

D has had this for decade(s): https://tour.dlang.org/tour/en/gems/uniform-function-call-sy...

Nim too has it: https://nim-by-example.github.io/oop/

Re: PHP 8.5 adds pipe operator

#173

This article makes a great case WHY the pipe operator is useful, but why didn't they just rewrite those functions to support method chaining? ` $profit = [1, 4, 5] .loadSeveral() .filter(isOnSale()) .map(sellWidget()) .array_sum(); ` this has the side benefit of 'looking normal'

Backwards compatibility. The language has done a pretty amazing job at adding features over the last 10 years without really breaking a lot of old code. I believe PHP still runs about 75% of the internet, so that's pretty huge.

Re: PHP 8.5 adds pipe operator

#174
post #142

I had this argument in the PHP community when the feature was being discussed, but I think the syntax is much more complicated to read , requiring backtracking to understand. It might be easier to write. Imagine you're just scanning code you're unfamiliar with trying to identify the symbols. Make sense of inputs and outputs, and you come to something as follows. $result = $arr |> fn($x) => array_column($x, 'values')…

This is what a good IDE brings to the table, it'll show that $result is of type string. The pipe operator (including T_BLING) was one of the few things I enjoyed when writing Hack at Meta.

> This is what a good IDE brings to the table, it'll show that $result is of type string.

I think the parent is referring to what the result _means_, rather than its type. Functional programming can, at times, obfuscate meaning a bit compared to good ol’ imperative style.

Re: PHP 8.5 adds pipe operator

#175
post #173

This article makes a great case WHY the pipe operator is useful, but why didn't they just rewrite those functions to support method chaining? ` $profit = [1, 4, 5] .loadSeveral() .filter(isOnSale()) .map(sellWidget()) .array_sum(); ` this has the side benefit of 'looking normal'

Backwards compatibility. The language has done a pretty amazing job at adding features over the last 10 years without really breaking a lot of old code. I believe PHP still runs about 75% of the internet, so that's pretty huge.

The Python 2 to 3 upgrade is a example of how important backwards compatibility is

Re: PHP 8.5 adds pipe operator

#176
post #62

Earlier quoted context omitted.

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.

https://www.php.net/manual/en/language.oop5.paamayim-nekudot... scope resolution operator

Re: PHP 8.5 adds pipe operator

#177
post #11

Earlier quoted context omitted.

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

Yeah, I agree. That's an advantage of pipes - although much harder to read and write than chained methods in my opinion. The use-case in the article could still be solved easier with extension methods in my opinion :-)

Yeah, the examples should also show that you can use arbitrary functions, not just library functions. E.g. your own business logic, validation etc.

Re: PHP 8.5 adds pipe operator

#178
post #150

Earlier quoted context omitted.

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.

Could you give a realistic example? The PHP pipes as described in the articles about it will require a bunch of wrapping anyway so you could just do that. There are several alternatives, from a function or method that just converts from raw array to class, to abstractions involving stuff like __invoke, __call, dispatchers and such. Also the expectation to not have to put facades on libraries is a bit suspicious, in m…

If I use Laravel’s Str in an app where both myself and a third party library want to add a chainable method, how do I make the API make sense? We can’t both subclass Str. The only option I see is Macroable and that’s a rabbit hole I’d rather avoid.

Re: PHP 8.5 adds pipe operator

#179

Earlier quoted context omitted.

So they are consistent because they are consistently inconsistent?? There isn't a good reason for PHP to have inherited C's issues here.

In the early days of PHP, it relied heavily on wrapping the underlying C libraries and preserving their naming conventions. https://news-web.php.net/php.internals/70950

And that was fine in the early days, absolutely.

We are not in the early days though, and in many other aspects PHP evolved greatly.

Re: PHP 8.5 adds pipe operator

#180

Rust is next? Jokes aside, pipe operators in programming languages have a interesting side effect of enabling railway oriented programming that I miss the most when not working in F#.

There is the `tap` crate (https://crates.io/crates/tap) which adds `tap`, `pipe` and their variants to everything.
Post reply on HN