Re: RFC Draft: Comprehensions
| From: | Andrea Faulds | Date: | Sun, 17 Mar 2019 19:46:58 +0000 |
| Subject: | Re: RFC Draft: Comprehensions | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104778@lists.php.net to get a copy of this message | ||
Hi,
Stanislav Malyshev wrote:
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. 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. Thanks, AndreaFinally, Python makes a distinction between list comprehensions using [] and generator expressions using (). This proposal effectively corresponds to generator expressions, but uses the [] syntax. I'm wondering if that will cause confusion.Do we need this distinction for anything useful?