On 19/03/2015 20:49, Alex Bowers wrote:
My proposal is something similar to Pythons slice, in PHP this would look
like:
$slided = $array[1:4]
This will get the elements in positions 1,2,3,4. (1 through 4 inclusive),
ignoring the actual key of the array. The result for an array will be an
array with the keys preserved, in the same order.
While I can see the reasoning for not looking into the actual keys
(which might not have a logically sliceable order), I think the syntax
needs to be more distinct, because this seems a bit confusing:
$countdown = [ 5 => 'Five!', 4 => 'Four!', 3 => 'Three!', 2 => 'Two!', 1
=> 'One!', 0 => 'Zero!' ]
$countdown[0]; // 'Zero!'
$countdown[0:0]; // ['Five!']
$countdown[0:][0] // 'Zero!'
$countdown[:0][0] // null - slice contains no key 0
The problem is that the slice access looks like an extension of key
access, but is actually a new concept of positional element access. With
a slight tweak to syntax, such as adding an @, we could make positional
access available for non-slices as well:
reset($countdown); // Five! - a common way of getting the first element
positionally
$countdown[@0]; // 'Five!'
$countdown[@1]; // 'Four!'
$countdown[@0:0] // ['Five!']
$countdown[@0:1] // ['Five!', 'Four!']
$countdown[@0:][@0] // 'Five!'
$countdown[@:0][@0] // 'Five!'
A side addition, that may be considered is adding [-1] to get the last item
in the array. But that is not a main part of this RFC.
This would actually be a really important addition for me, but only if I
can do it on a non-slice, as above:
$countdown[-1]; // null - no such key
end($countdown); // 'Zero!' - common way of getting last element
$countdown[@-1]; // 'Zero!' - positional access counting from the end
$countdown[@-2]; // 'One!' - fiddly to do in current PHP
I just wrote out all those examples and realised that it may not be
possible to reuse @ in this context. Finding new symbols to use for
syntax is hard. :(
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php