Hi,

Stanislav Malyshev wrote:

Finally, 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?


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,
Andrea

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to