Re: [RFC] Allow specifying keys in list()
| From: | Leigh | Date: | Tue, 19 Jan 2016 12:55:13 +0000 |
| Subject: | Re: [RFC] Allow specifying keys in list() | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-90714@lists.php.net to get a copy of this message | ||
On 19 January 2016 at 02:20, Andrea Faulds <ajf@ajf.me> wrote:
> After considering how to implement this at the opcode level, I've decided
> again that it's not worth it. Mixing keyed and unkeyed elements is not only
> an implementation nuisance (it's not necessarily hard, but it makes things
> more complicated), it's not something likely to be used in practice, and I
> think it's probably an indicator of bad code.
>
> Now, I can understand your case, but I don't really want to add a special
> exception for it. If we're allowing keyed or unkeyed, not both, then we
> should stick to it.
>
> Also, I realised that in your case (
list(7 => $a, $b, $c,
> $d)), there are
> other, possibly cleaner ways to do it, such as:
>
> list($a, $b, $c, $d) = array_slice($array, 7);
>
> This way is simple and clear. It's probably clearer than `list(7 => $a, $b,
> $c, $d)` in fact. There's also this:
>
> list(($i = 7) => $a, ++$i => $b, ++$i => $c, ++$i => $d) = $array;
>
> This way is horrible, but at least demonstrates there are other ways to
> achieve what you want.
Thanks, I wouldn't worry about it. The use-case was hypothetical
anyway. I could see how I might use it, but I have nowhere at present
I'd actually use it.
list($offset => $a, $b, $b, $c) = $array;
$offset += 4;
vs.
list($a, $b, $c, $d) = array_slice($array, $offset);
$offset += 4;
Probably not worth the complexity overhead as you suggest, for what
amounts to (arguably) syntactic sugar. I also doubt it would see
enough usage to warrant the small gain of avoiding the fcall there.