StructTypes.jl
StructTypes.jl copied to clipboard
Recommendations how to add subtypes later
Assuming i have a have piece of code which has an StructTypes.AbstractType() which already has it's StructTypes.subtypes(::Type{Vehicle}) set but i now want to add a new subtype at run time.
Is there a recommended way to do that? If so i would offer to extend the documentation with an example. My current solution would be StructTypes.subtypes(::Type{Vehicle}) = merge(StructTypes.subtypes(::Type{Vehicle}), (; train=Train)) but if there is a different way preferred with the package then i would like to know.
I'm curious if there's a better solution, but I put this together yesterday. My understanding is that InteractiveUtils.subtypes is slow, but for my use case it doesn't matter.
function concrete_subtypes(T)
out = Type[]
for S in subtypes(T)
isconcretetype(S) ? push!(out, S) : append!(out, concrete_subtypes(S))
end
return out
end
StructTypes.subtypes(::Type{T}) where {T <: MyAbstractType} = Dict(T.name.name => T for T in concrete_subtypes(T))