[Feature Request] [stdlib] [proposal] Have all fn __getitem__(self, span: Slice) -> Self
return Iterator[Self]
instead
#3653
Labels
fn __getitem__(self, span: Slice) -> Self
return Iterator[Self]
instead
#3653
Review Mojo's priorities
What is your request?
Once we have Iterators designed or right now using each type's specialized iterator, change all
fn __getitem__(self, span: Slice) -> Self
to return the iterator and add constructors that take that iterator so that implicit conversion is possible.What is your motivation for this change?
Te idea came to me while reading PR #3650 which allocates a buffer to return a negatively stepped slice over a readonly view of data that the
Span
is built upon which doesn't make much sense but there is really no alternative since the function should return an instance ofSelf
.Iterating with a particular step and in any particular direction is much simpler and doable without copying buffers. There is also the added benefit that when people do:
when building an instance of the type with that iterator it would only allocate when the type is an owning type or when
step != 1
for non-owning typesThis will also (I think) benefit performance in the case of
String
slicing which currently does not do it by unicode codepoints but will in the future, which needs to count all previous codepoints and then from then count untilend - start
of the slice is reached, allocate a buffer for the new string + new null terminator, and return. This iterator approach would mean just counting until the beginning of the slice and returning an iterator with the givenend - start
length and the given step.Any other details?
The only downside this would have is that when wanting to do stuff with the type after slicing is that the API from the iterator is nonexistent, so you'd need to build an instance explicitly.
The text was updated successfully, but these errors were encountered: