ArrayInterface.jl icon indicating copy to clipboard operation
ArrayInterface.jl copied to clipboard

AbstractRange traits

Open Tokazama opened this issue 5 years ago • 5 comments

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.

Tokazama avatar Apr 17 '20 09:04 Tokazama

Yes, that could be useful here as well.

ChrisRackauckas avatar Apr 18 '20 16:04 ChrisRackauckas

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.

Tokazama avatar Apr 20 '20 14:04 Tokazama

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 avatar Jul 14 '20 05:07 chriselrod

@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.

Tokazama avatar Jul 14 '20 05:07 Tokazama

I agree.

ChrisRackauckas avatar Jul 14 '20 14:07 ChrisRackauckas