Re: RFC Draft: Comprehensions

From: Date: Tue, 19 Mar 2019 22:46:35 +0000
Subject: Re: RFC Draft: Comprehensions
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-104811@lists.php.net to get a copy of this message
Hi! > In Python, the difference is that []-syntax gives you a list > (pre-comupted), whereas without the [] you get a generator > (generate-on-demand). This distinction is important because the > generator might be iterating over something non-repeatable (e.g. another > generator), or have some destructive or mutating effect. You also might > not want to take the performance penalty of computing it every time you > iterate over it. Understandable, but I wonder how important it is for the short syntax implied by list comprehensions? I mean, there could be all kinds of complex cases, but those are easily doable by using already existing functional syntax. If you need a generator over non-repeatable sequence, you can get it now with a functional syntax. But in my experience, about 99% times I use this syntax (in Python) is a simple sequence filter and I want to get a list out of it. I suspect PHP usage would be the same. So I wonder if it's worth to bother inventing special syntax for something that will be used in 1% of cases, and even in those cases might be more readable as a generator function? > Why not apply the same approach to PHP? There is iterator_to_array() to > convert a generator to an array, so we may not need both syntaxes. > However, I think using [] for something that is *not an array* is > counter-intuitive. No, I would definitely be for []-syntax producing an array. As I said, that's in my experience what people usually want. -- Stas Malyshev smalyshev@gmail.com

« previous php.internals (#104811) next »