The Pipe Operator

Read the RFC Externals
90
262 yes
126 no

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

$result = "Hello World"
    |> 'htmlentities'
    |> 'str_split'
    |> fn($x) => array_map('strtoupper', $x)
    |> fn($x) => array_filter($x, fn($v) => $v != 'O');

This RFC was already declined, but we're sharing it here as another test RFC, and because it'd be interesting to learn people's opinion about it.

Click the bar to cast your vote!
68%
32%
1

My chief complaint about the syntax is it blocks functions with multiple parameters. This kneecaps the piping capability, and you're forced back into inside-out calls, or stopping a pipe to call a prohibited function, then start a new pipe with that result.

The operator itself |> is fine, and to me it reads easily as one expression sending a value forward. What i dislike is the representation of the right-side callable: " Hello world " |> 'trim' or "Hello world" |> [$object, 'method'] ... In my opinion, the language already has too much of this workaround syntax.

First-class callable syntax " Hello world " |> trim(...) is helpful in that regard, but still falls apart when multiple arguments are needed.

Share:
thookerov avatar
thookerov
voted no
1

Generally YES, but with some minor change that would allow skipping defining function, so instead of:

$result = "Hello World"
    |> htmlentities(...)
    |> str_split(...)
    |> fn($x) => array_map(strtoupper(...), $x)
    |> fn($x) => array_filter($x, fn($v) => $v != 'O');

I'd rather see something like

$result = "Hello World"
    |> htmlentities(...) // or htmlentities(?)
    |> str_split(...)
    |> array_map(strtoupper(...), ?)
    |> array_filter(?, fn($v) => $v != 'O');
Share:
jacek avatar
jacek
voted no
1

This looks like a problem that would be better-solved with a first-party object-oriented API for primitive types (a bit like JavaScript). Honestly the syntax looks a bit odd and as per the example, referencing global functions as strings is an anti-pattern IMO (not friendly for IDEs / refactoring!).

For example:

$result = ("Hello World") // string
    ->htmlentities(...)
    ->split(...) // array
    ->map(fn ($x) => strtoupper($x))
    ->filter(fn($v) => $v != 'O');
Share:
uphlewis avatar
uphlewis
voted no
1

It's nice to have.

Share:
stanislav-janu avatar
stanislav-janu
voted yes
1

I don't see any advantages and the syntax is a mess.

Share:
uli avatar
uli
voted no
1

functions as strings? Bro no way

Share:
pimjansen avatar
pimjansen
voted no
1

Improves readabillity drastically

Share:
aradoje avatar
aradoje
voted yes
1

Neat with first class callable syntax

Share:
sandermuller avatar
sandermuller
voted yes
1

It makes chain calls easier

Share:
maureis avatar
maureis
voted yes
1

First, it's really mostly syntactic sugar, adding cognitive load for no added Language-level feature. Besides, it's not clear how easy it would be to step over this code with a debugger.

Second, the argument that the equivalent nested function calls is much less readable, is sound but I think a developer should write neither. Both nested functions calls and chained function calls are hard to live-debug and hard to read, and most of the time in my experience are also an unnecessary micro-optimization.

Third, I think this specific operator "|>" isn't easy to type. The "->" member operator was a mistake of the past, we are stuck with it, don't make another one.

Share:
agc avatar
agc
voted no
1

Makes code more readable because has the natural direction of reading (if reading left-to-right, of course)

Share:
daviscaics avatar
daviscaics
voted yes
1

There is a chain of responsibilities patter that does exactly the same, so I don't see any reason to implement this.

Share:
piotrfilipek avatar
piotrfilipek
voted no
1

This doesn't appear in any popular language and does not bring any benefit to readability.

Share:
riki137 avatar
riki137
voted no
1

this actually looks not pretty

Share:
sunxyw avatar
sunxyw
voted no
1

I think it's a very nice way to simplify code. For me it looks readable and cleaner without the need of creating temporal variables.

Share:
marcmorente avatar
marcmorente
voted yes

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

38
51 yes
13 no
Interface Default Methods

Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.

95
168 yes
264 no
Property Hooks

A new way of interacting with properties

65
125 yes
81 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!