Re: RFC - Array slice syntactic sugar
| From: | Rowan Collins | Date: | Fri, 20 Mar 2015 12:24:22 +0000 |
| Subject: | Re: RFC - Array slice syntactic sugar | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-85278@lists.php.net to get a copy of this message | ||
Alex Bowers wrote on 20/03/2015 11:38:
Yep, I think we're on the same page now. There's basically three ways of selecting items - by key, by position from start, and by position from end - and two types of selection - a single element, or an array of zero or more elements. That gives us 6 possible combinations, which (using the @ notation for consistency with previous examples) would be: Select by key: Single element: $array[$x] - existing syntax Slice: $array[$a:$b] - equivalent to a filter asserting $key >= $a && $key <= $b Select by position from start: Single element: $array[@$x] Slice: $array[@$a:$b],$array[@$a:], and $array[@:$b] Select by position from end - as above, but using negative indexes: Single element: $array[@-$x] - equivalent to $array[@length($array)-$x] Slice: $array[@-$a:-$b] - equivalent to $array[@length($array)-$a:length($array)-$b] Several of these are actually rather hard to do with current PHP features: - you can get the first element with reset() and the last with end(), but arbitrary position selection is harder - there is no version of array_filter for testing keys, and being able to write $dictionary['elephant':'snake'] would actually be pretty handy Actually, the only one that's easy to emulate (apart from the obvious single element by key) is the one you propose to add first - getting a positional slice of an array, which array_slice(..., true) already does. Regards, -- Rowan Collins [IMSoP]The @ symbol in my examples is not a special marker that is meaningful on its own; the parser wouldn't even see it as a separate token. The syntax for key access is $array[$key], the syntax for positional access would be $array[@$position]; chosen to look similar, but one is not a special case of the other.Ah, I understand you now. Yes I agree that this would be useful then. It allows us the future possibility of a separate RFC looking into slicing on keys as mentioned at the bottom of your post.The syntax needn't be just an extra character, we could invent something else, like $array[[$position]], or $array@($position), though I can't think of anything I like.I think for simplicity, we should keep it as $array[], and have a symbol within it that shows that it is an internal index rather than the key. An Asterisk (*) is my preference at the moment, though caret also looks promising. Here are some examples of how various characters could look $array[*1:3], $array[*:], $array[*:1], $array[*1:] $array[@1:3], $array[@:], $array[@:1], $array[@1:] $array[^1:3], $array[^:], $array[^:1], $array[^1:] $array[&1:3], $array[&:], $array[&:1], $array[&1:]The symbol isn't something that can be missing, it's just part of the syntax, so this is like saying "what should we do if the closing ] is missing?" The answer is, nothing, it's undefined syntax, so it's a parse error.So a parse error currently, and a future RFC can define the terms of using this normally? If we go down the route of adding a symbol for by index rather than by key, then this RFC should also include the normal getting array by key style, without the colon range operator. For example: $array[*1] to get the item that is the second in the list, rather than at key 1.