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

AnnotatedString constructor too restrictive #55245

Open
aplavin opened this issue Jul 25, 2024 · 4 comments · May be fixed by #55741
Open

AnnotatedString constructor too restrictive #55245

aplavin opened this issue Jul 25, 2024 · 4 comments · May be fixed by #55741
Labels
strings "Strings!"

Comments

@aplavin
Copy link
Contributor

aplavin commented Jul 25, 2024

julia> anns = [:x => "a", :y => 1]

# this works:
julia> Base.AnnotatedString("abc def", [(i:i, anns[i]) for i in 1:2])
"abc def"

# but after replacing 2 with 0 doesn't:
julia> Base.AnnotatedString("abc def", [(i:i, anns[i]) for i in 1:0])
ERROR: MethodError: no method matching Base.AnnotatedString(::String, ::Vector{Tuple{UnitRange{Int64}, Any}})

I think the constructor type constraints are too restrictive.

@nsajko
Copy link
Contributor

nsajko commented Jul 25, 2024

julia> eltype([(i:i, anns[i]) for i in 1:0])
Tuple{UnitRange{Int64}, Any}

julia> eltype([(i:i, anns[i]) for i in 1:2])
Tuple{UnitRange{Int64}, Pair{Symbol, Any}}

You could specify the element type when constructing the vector, though.

@KristofferC
Copy link
Sponsor Member

Seems better to verify during construction than to restrict the container type, or?

@tecosaur
Copy link
Contributor

We could define something like

AnnotatedString(str::AbstractString, itr) =
    AnnotatedString(str, Tuple{UnitRange{Int64}, Pair{Symbol, Any}}(collect(itr)))

Thoughts?

@tecosaur
Copy link
Contributor

If that seem good, I'll happily whip up a PR.

@nsajko nsajko added the strings "Strings!" label Aug 11, 2024
@tecosaur tecosaur linked a pull request Sep 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
strings "Strings!"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants