JuliaData / StructTypes.jl

Abstract definitions and convenience methods for describing, processing, and constructing Julia objects
https://juliadata.github.io/StructTypes.jl/stable/
MIT License
79 stars 22 forks source link

Combining abstract types and parametric types? #76

Open heyx3 opened 2 years ago

heyx3 commented 2 years ago

Let's say I have a type hierarchy like this:

abstract type Parent end
mutable struct Child1 <: Parent
    type # Set to 'child1'
end
mutable struct Child2{N<:Integer} <: Parent
    type # Set to 'child2'
    i::N
end

StructTypes.StructType(::Type{Parent}) = StructTypes.AbstractType()
StructTypes.StructType(::Type{Child1}) = StructTypes.Mutable()
StructTypes.StructType(::Type{<:Child2}) = StructTypes.Mutable()

StructTypes.subtypekey(::Type{Parent}) = :type
StructTypes.subtypes(::Type{Parent}) = (child1=Child1, child2=Child2)

Currently, afaik, there is no mechanism for StructTypes to serialize/deserialize the value of N, like how it can write the type key for each child.

Current workarounds I can think of:

Would it be feasible to support deserializing type parameters in the same way as the type itself? For example:

struct Child2{N<:Integer} <: Parent
    type # Set to 'child2'
    int_type::Type # Set to string(N)
    i::N
end
StructTypes.parametrickey(::Type{<:Child2}, ::Val{1}) = :int_type