On Tue, Mar 19, 2019, at 7:10 PM, Robert Hickman wrote:
> > > 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.
> >
> 
> In python comprehensions on [] makes a list and comprehensions in {}
> make a dictionary (list and dict comprehensions). As PHP only has one
> 'array' type using [] makes sense. Along that train of thought, should
> comprehensions also be possible in the old array() syntax?

Honestly, I cannot think of any case where I'd use a comprehension where I 
would definitely want an array and not a generator.  In the majority case both 
work equally well, cool, but I don't know when I would even use an 
array-dependent version.  And converting from a generator to an array is 
trivial; the other way, very not so much.

> Does the proposal include comprehensions which build associative
> arrays? In python you can do {a['key'] : a for a in lst}, where lst is
> a list of dicts. I often use this to create a dictionary whose keys
> index a sub-element of a nested dictionary, as my example shows.

The proposal as written, yes, you can produce values or keys-and-values.

I would still love feedback on possible alternative syntaxes, per my earlier 
email.

--Larry Garfield

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

Reply via email to