Add AbstractOneTo
and have OneTo
be its subtype
#56902
Open
+47
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
Base
definessimilar
forBase.OneTo
, with the understanding that offset axes will be handled elsewhere. However,Base.OneTo
is just one possible one-based range, and there are others such asStaticArrays.SOneTo
that exist in the ecosystem.Base
doesn't provide a method to handle a combination of different one-based ranges insimilar
, which leaves the packages in an awkward position: they need to define methods likewhere
HeterogeneousShapeTuple
is defined asTuple{Vararg{Union{Integer, Base.OneTo, SOneTo}}}
https://github.com/JuliaArrays/StaticArrays.jl/blob/07c12450d1b3481dda4b503564ae4a5cb4e27ce4/src/abstractarray.jl#L141-L146
Unfortunately, such methods are borderline type-piracy, as noted in JuliaArrays/StaticArrays.jl#1248. In particular, if the narrower
Base
method that handlesUnion{Integer, OneTo}
is removed, then this method explicitly becomes pirating.A solution to this situation is to have
Base
handle all one-based ranges, such that arbitrary combinations of one-based ranges hit fallback methods inBase
. This PR is a first step in this direction. We add the abstract typeAbstractOneTo
, and haveOneTo
be its subtype. We also add methods tosimilar
andreshape
that acceptAbstractOneTo
arguments. This makes it unnecessary for packages to dispatch on awkward combinations ofUnion{Integer, OneTo}
and custom one-based axes, as the base implementation would handle such cases already.There may be other methods that accept an
AbstractOneTo
instead of aOneTo
, but these may be addressed in separate PRs. Also, there may be one-based ranges that can't subtypeAbstractOneTo
, and a full solution that accepts such ranges as well needs to be implemented through a trait. This may also be handled in a separate PR.