I think a NamedTuple can do what I want but I can’t quite figure out the syntax. Can someone help me with this bit of runnable code?
function nameparts(v)
t = split(v,':')
length(t) < 2 ? (nothing,t[1]) : (t[1],t[2])
end
struct QName{T<:AbstractString}
pfx::Union{T,Nothing}
nm::T
end
QName(nm::AbstractString) = QName(nameparts(nm)...)
struct ID{T<:AbstractString}
id::T
end
attr = Dict("base"=>"xs:string")
atts = @NamedTuple{base::QName, id::Union{Nothing,ID}}
s = string.(fieldnames(atts))
vals = get.(Ref(attr), s, nothing) #vals will be strings or nothing
@show vals
# here I want to instantiate the types
# to create a result tuple that looks like
# `(QName{SubString{String}}("xs", "string"), nothing)`
maybe = atts(vals)
But it bombs with
ERROR: MethodError: Cannot
convert
an object of type
String to an object of type
QName
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:171
QName(::AbstractString) at /Users/doug/dev/XSDJ/src/ex.jl:10
QName(::Union{Nothing, T}, ::T) where T<:AbstractString at /Users/doug/dev/XSDJ/src/ex.jl:7
Stacktrace:
[1] convert(::Type{Tuple{QName,Union{Nothing, ID}}}, ::Tuple{String,Nothing}) at ./essentials.jl:310
[2] Tuple{QName,Union{Nothing, ID}}(::Tuple{String,Nothing}) at ./tuple.jl:225
[3] NamedTuple{(:base, :id),Tuple{QName,Union{Nothing, ID}}}(::Tuple{String,Nothing}) at ./namedtuple.jl:90
[4] top-level scope at /Users/doug/dev/XSDJ/src/ex.jl:28
[5] include(::String) at ./client.jl:457
[6] top-level scope at REPL[13]:1
Do I need to implement a convert
method?