Skip to content

AnnotatedStrings: unclear what operations support/preserve annotations #55401

Open
@aplavin

Description

Not sure whether I really understand the logic: where annotations are supported and where they aren't. Would be nice to document it... Some of these may be bugs, but hard to tell without knowing the intentions.

julia> a = Base.AnnotatedString("abc", [(2:3, :x => 123)])
julia> av = view(a, 1:2)

1:

# annotations present in both a and av...
julia> Base.annotations(a)
1-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
 (2:3, :x => 123)

julia> Base.annotations(av)
1-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
 (2:2, :x => 123)

# ... but not when putting them into an array
julia> Base.annotations.([a,av])
2-element Vector{Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}}:
 [(2:3, :x => 123)]
 []

2:

# regex matching preserves annotations...
julia> first(eachmatch(r"\w", a)).match |> Base.annotations
Tuple{UnitRange{Int64}, Pair{Symbol, Any}}[]

# ... but not always
julia> first(eachmatch(r"\w", av)).match |> Base.annotations
ERROR: MethodError: no method matching annotations(::SubString{String})

# and sometimes matching doesn't work at all:
julia> match(r"\w", av)
ERROR: ArgumentError: regex matching is only available for the String and AnnotatedString types; use String(s) to convert
# match() actually supports substrings, so I guess the error message is wrong?

3:

# replace() works, but silently drops annotations:
julia> replace(a, a=>a) |> Base.annotations
ERROR: MethodError: no method matching annotations(::String)

4:

# string multiplication preserves annotations,
julia> (a * av) |> Base.annotations
1-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
 (2:3, :x => 123)

# join() does as well ...
julia> join([a, av]) |> Base.annotations
1-element Vector{Tuple{UnitRange{Int64}, Pair{Symbol, Any}}}:
 (2:3, :x => 123)

# ... but string concatenation doesn't
julia> string(a, av) |> Base.annotations
ERROR: MethodError: no method matching annotations(::String)

# interpolation also drops annotations:
julia> "x $a $av" |> Base.annotations
ERROR: MethodError: no method matching annotations(::String)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions