Prospective changes to AnnotatedString and StyledStrings

I may have misunderstood your advice @tecosaur.

In my package extension to XLSX.jl, I’ve written:

setdata!(sheet::Worksheet, ref::CellRef, ss::AnnotatedString{T}) where T = setdata!(sheet, ref, RichTextString(_ssToRuns(ss)))

@static if VERSION >= v"1.14-"
        const _SS_ARG = Union{<:AnnotatedString{>:Face}, SubString{<:AnnotatedString{>:Face}}}
    else
        const _SS_ARG = Union{<:AnnotatedString, SubString{<:AnnotatedString}}
    end

function _ssToRuns(s::_SS_ARG)
    # Do some stuff
end

It works perfectly for pre-1.14.

However, in v1.14, I get

ERROR: MethodError: no method matching _ssToRuns(::AnnotatedString{String})
The function `_ssToRuns` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _ssToRuns(::Union{AnnotatedString{>:StyledStrings.Face}, SubString{<:AnnotatedString{>:StyledStrings.Face}}})
   @ StyledStringsSstsExt C:\Users\Tim Gebbels\OneDrive\Documents\Julia\XLSX v0.11\XLSX.jl\ext\StyledStringsSstsExt.jl:38

In the REPL, I can do:

julia> using StyledStrings, XLSX

julia> s=styled"{yellow:hello} {blue:there}"
"hello there"

julia> typeof(s)
AnnotatedString{String}

julia> s isa AnnotatedString{>:StyledStrings.Face}
false

I’m generally not very clever at understanding Julia’s type system and I’ve obviously got something wrong here.

Is there anything obvious you can suggest?

I’ve got things working now.

As suggested in the StylesStrings.jl docs, I’ve opened an issue to request making some currently internal functions public.

For anybody wondering what’s happened to Face primacy and palettes - Pull Request #131 - JuliaLang/StyledStrings.jl - GitHub, the short answer is that making sure that the particular design compromises are right (no piracy, no invalidations, not digging a hole for ourselves, etc.) has been difficult.

I’ve continued coming back to this problem though (and bothering Cody), and I think this set of changes in now in a pretty decent state.

Since I’ve been thinking hard about the guts of the code, I’ve also landed a few performance improvements on top, ending up with ~5x the performance of 1.13’s StyledStrings.jl, which actually has us beating printstyled for all but the most trivial cases (i.e. print a single coloured word once, that’s it) even with all of the extra capabilities.

With the PR, it’s also possible to use StyledStrings.jl with JuliaC in --trim=safe mode.

I’m pretty chuffed with this.