Open ysbaddaden opened 2 days ago
For Slice
there is also #12617
I suppose we should utilize Indexable.normalize_start_and_count
in Slice#[]?
. The purpose of this helper method is exactly to synchronize this behaviour across implementations.
We actually use it already in Slice#fill
. However, there is also a comment mentioning the restricted behaviour of Slice#[]
regarding count
.
And yeah, I find the implicit clamping of count
can be a bit surprising. I would expect the result of slice[i, 8]
to have 8 elements, not "up to 8" (Math.min(slice.size - i, 8)
). I'd probably expect the same for Array
as well. 🤷
While trying get the last 3 bytes in a slice, I noticed that
bytes[-3, 3]
failed unexpectedly. Digging a bit, I noticed that there is a discrepancy in how[](start, count)
with a negative start behaves in the different array-like collections:Array
: returnscount
entries starting from the laststart
entries (expected);Deque
: not implemented;Slice
: raises anIndex out of bounds
exception (huh?).Given that
[](index)
with a negative index is behaving the same in all these types (return item at index starting from the end), I'd expect[](count, start)
to behave the same, too.The method isn't implemented for
StaticArray
but it's likely deliberate (aka not possible). It may not make much sense forDeque
either (though possible), butSlice
was surprising.