Re: RFC Draft: Comprehensions

From: Date: Wed, 20 Mar 2019 00:20:03 +0000
Subject: Re: RFC Draft: Comprehensions
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-104813@lists.php.net to get a copy of this message
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

« previous php.internals (#104813) next »