Skip to content
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

should @view work for strings? #39657

Closed
stevengj opened this issue Feb 14, 2021 · 2 comments
Closed

should @view work for strings? #39657

stevengj opened this issue Feb 14, 2021 · 2 comments
Labels
speculative Whether the change will be implemented is speculative strings "Strings!"

Comments

@stevengj
Copy link
Member

stevengj commented Feb 14, 2021

Is there some reason why @view and @views don't produce SubString objects?

julia> s = "αβγ"
"αβγ"

julia> s[3:5] # a copy
"βγ"

julia> @view s[3:5]
ERROR: MethodError: no method matching view(::String, ::UnitRange{Int64})
Closest candidates are:
  view(::AbstractUnitRange, ::AbstractUnitRange{var"#s91"} where var"#s91"<:Integer) at subarray.jl:167
  view(::StepRange, ::AbstractRange{var"#s91"} where var"#s91"<:Integer) at subarray.jl:175
  view(::StepRangeLen, ::OrdinalRange{var"#s91",S} where S where var"#s91"<:Integer) at subarray.jl:179
  ...
Stacktrace:
 [1] top-level scope at REPL[25]:1

julia> SubString(s, 3:5) # I wish this was @view s[3:5]
"βγ"

julia> @views s[3:5]  # actually returns a copy
"βγ"

julia> typeof(ans)
String

Fixing @view would be a non-breaking change since it currently throws an error. Fixing @views would technically be breaking but I'm skeptical that it affects any real code?

@stevengj stevengj added speculative Whether the change will be implemented is speculative strings "Strings!" labels Feb 14, 2021
@simeonschaub
Copy link
Member

Does this not already work on nightly? (Ref #35879)

@stevengj
Copy link
Member Author

stevengj commented Feb 14, 2021

Oh, great! Yes, it is fixed on nightly, I totally forgot about that PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative strings "Strings!"
Projects
None yet
Development

No branches or pull requests

2 participants