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

Recommendations how to add subtypes later #96

Open
freemin7 opened this issue Aug 29, 2023 · 1 comment
Open

Recommendations how to add subtypes later #96

freemin7 opened this issue Aug 29, 2023 · 1 comment

Comments

@freemin7
Copy link

freemin7 commented Aug 29, 2023

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.

@joshday
Copy link

joshday commented Aug 29, 2023

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants