Allow AbstractUnitRange for string getindex#41807
Conversation
Looks like another method signature update, that was also simply too strict previously, is needed here julia/base/strings/substring.jl Line 255 in 5ff9e3a |
|
What does julia> r = OffsetArrays.IdOffsetRange(3:5, -2)
OffsetArrays.IdOffsetRange(values=1:3, indices=-1:1)
julia> r isa AbstractUnitRange
true
julia> first(r)
1
julia> firstindex(r)
-1See https://juliaarrays.github.io/OffsetArrays.jl/stable/internals/#The-axes-of-OffsetArrays and the "fundamental axiom of generalized indexing." Easy fix: add a |
The first question should be, "what did it do?". Here's the behavior on Julia 1.6.0: julia> VERSION
v"1.6.0"
julia> using OffsetArrays
julia> s = "Tim Holy"
"Tim Holy"
julia> r = OffsetArrays.IdOffsetRange(3:5, -2)
OffsetArrays.IdOffsetRange(values=1:3, indices=-1:1)
julia> s[r]
"Tim"
julia> s[OffsetArray(1:3, -1:1)]
"Tim"
julia> @which s[r]
getindex(s::AbstractString, v::AbstractVector{var"#s79"} where var"#s79"<:Integer) in Base at strings/basic.jl:192
julia> Base.getindex(s::String, r::AbstractUnitRange{<:Integer}) = s[Int(first(r)):Int(last(r))]
julia> s[r]
"Tim"
julia> @which s[r]
getindex(s::String, r::AbstractUnitRange{var"#s1"} where var"#s1"<:Integer) in Main at REPL[11]:1 |
|
Interesting! A bit unexpected, but I guess there is an argument for strings that simply says they don't pay attention to the indexes of the indexes, but only to the values. 👍 |
|
Strings also don't pay attention to the values either, just the end points. They don't really act like arrays when indexed. |
Several methods could be generalized to work with
AbstractUnitRangerather than justUnitRange. There are now severalAbstractUnitRangesubtypes:This pull request allows
AbstractUnitRangeforgetindexon aString.There seem to be some ambiguous method errors associated with the current change.