Re: [RFC] Spread Operator in Array Expression v0.2
| From: | Rowan Collins | Date: | Thu, 04 Apr 2019 16:39:33 +0000 |
| Subject: | Re: [RFC] Spread Operator in Array Expression v0.2 | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-105081@lists.php.net to get a copy of this message | ||
On Thu, 4 Apr 2019 at 17:14, Derick Rethans <derick@php.net> wrote:
> Could you add to the RFC what the exact pain point is that this is
> trying to address? It looks a little like this is just adding syntax for
> the sake of it.
>
Not everything is about pain, some things are just about gain. ;)
The link Levi shared about Dart included some interesting examples of where
spreads are useful, some of which you can probably imagine happening in
PHP:
https://medium.com/dartlang/making-dart-a-better-language-for-ui-f1ccaf9f546c
It also takes us a step closer to having a short-hand for
iterator_to_array, in the shape of [...$iterator]. On its own, that's still
pretty ugly, but it's not hard to come up with cases where it would be a
lot nicer, like concatenating two iterators:
// Before
array_merge(iterator_to_array($iter1), iterator_to_array($iter2))
// Or to generalise to all iterables
array_merge( is_array($iter1) ? $iter1 : iterator_to_array($iter1),
is_array($iter2) ? $iter2 : iterator_to_array($iter2) )
// After (handles both cases)
[ ...$iter1, ...$iter2 ]
Granted, I can't point to a real-life example of that, but it shows that
this isn't just new syntax for something that's already easy.
Regards,
--
Rowan Collins
[IMSoP]