-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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)
-1 See 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
AbstractUnitRange
rather than justUnitRange
. There are now severalAbstractUnitRange
subtypes:This pull request allows
AbstractUnitRange
forgetindex
on aString
.There seem to be some ambiguous method errors associated with the current change.