ArrayInterface.jl
ArrayInterface.jl copied to clipboard
AbstractRange traits
Would it be within the scope of this package to address range traits? I have several ideas that I figured would be more useful here than in a separate package.
Yes, that could be useful here as well.
The only trait in base we have is RangeStepStyle which isn't extensively used and has variable interpretations (see https://github.com/JuliaLang/julia/pull/35279).
It may also be useful to have traits that are based on how a range is constructed. For example, there's variability in how the start of a range is determined (e.g., Base.Oneto always begins at one, UnitRange and StepRange always begin at range.start, StepRangeLen is derived from a reference value and an offset). There is similar variability in how the last element of a range is determined. These would be helpful for creating fast look up/indexing methods without worrying about the type hierarchy of of AbstractRange as much.
What about something for extracting static/known size information? Something like:
known_start(::Any) = nothing
known_stop(::Any) = nothing
known_start(::Type{<:Base.OneTo}) = 1
# As an example, StaticArrays could add support via:
using StaticArrays
known_start(::Type{<:StaticArrays.SOneTo}) = 1
known_stop(::Type{<:StaticArrays.SOneTo{N}}) where {N} = N
@chriselrod I like this approach a lot. It would also be worth having
known_step(::Any) = nothing
known_step(::Type{<:AbstractUnitRange{T}}) where {T} = one(T)
Just these three simple traits would make it a lot easier to write generic methods for ranges.
I agree.