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

Allow for generically extracting unannotated string #55458

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ A string with metadata, in the form of annotated regions.

More specifically, this is a simple wrapper around any other
[`AbstractString`](@ref) that allows for regions of the wrapped string to be
annotated with labeled values.
annotated with labelled values. The underlying string can be extracted by
calling a string constructor with the `AnnotatedString` as the argument.

```text
C
Expand Down Expand Up @@ -125,7 +126,10 @@ AnnotatedString(s::AnnotatedString, annots::Vector{Tuple{UnitRange{Int}, Pair{Sy
AnnotatedChar(c::AnnotatedChar, annots::Vector{Pair{Symbol, Any}}) =
AnnotatedChar(c.char, vcat(c.annotations, annots))

String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead
# To allow for generically de-annotating a string.
(::Type{T})(s::AnnotatedString{T}) where {T <: AbstractString} = T(s.string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is genuinely ambiguous with respect to at least these methods, possibly more:

GenericString(string::AbstractString) @ Base strings/annotated.jl:130
SubString(s::AbstractString) @ Base strings/substring.jl:59
Core.Compiler.LazyString(args...) @ Core.Compiler strings/lazy.jl:41

I am not sure it is possible to resolve this (esp. if one also takes the potential for similar such issues with packages into account). Not am I sure it is necessary: sure instead of MyStringType(my_annotate_str) it is also OK if I have to write MyStringType(String(my_annotate_str)) ? The necessary method is already in master

String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead (and avoid ambiguity)
AnnotatedString(s::AnnotatedString) = s # To resolve an ambiguity

## Conversion/promotion ##

Expand Down