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

Macro for subtypes lowering with custom type field #93

Closed
sairus7 opened this issue May 18, 2023 · 1 comment · Fixed by #94
Closed

Macro for subtypes lowering with custom type field #93

sairus7 opened this issue May 18, 2023 · 1 comment · Fixed by #94

Comments

@sairus7
Copy link
Contributor

sairus7 commented May 18, 2023

According to this solution for lowering subtypes that does not have additional type field:
The need for a type::String field and subtypekey for abstract types

I've tried to write such a macro, but I think I get this wrong, can you look at it? @quinnj

macro lowersubtype(type, subtypekey)
    T = Core.eval(__module__, type)
    vars = [QuoteNode(e) for e in fieldnames(T)]
    xvars = [:(x.$(e)) for e in fieldnames(T)]
    types = [esc(e) for e in fieldtypes(T)]
    nt = :(NamedTuple{(QuoteNode($subtypekey), $(vars...),), Tuple{String, $(types...)}})
    return quote
        StructTypes.StructType(::Type{$(esc(type))}) = StructTypes.CustomStruct()
        StructTypes.lower(x::$(esc(type))) = (cmd = $subtypekey, $(xvars...))
        StructTypes.lowertype(::Type{$(esc(type))}) = $nt
        $(esc(type))(x::$nt) = $(esc(type))($(xvars...))
    end
end

struct Foo
    x::Int
    y::Float64
end

@lowersubtype Foo "type"
@sairus7 sairus7 changed the title Macro for subtypes lowering with custom type key Macro for subtypes lowering with custom type field May 18, 2023
@sairus7
Copy link
Contributor Author

sairus7 commented May 18, 2023

Finally got this working:

macro register_subtype(struct_type, field_name, field_value)
    T = Core.eval(__module__, struct_type)
    x_names = [:(x.$(e)) for e in fieldnames(T)] # x.a, x.b, ...
    name_types = [:($n::$(esc(t))) for (n, t) in zip(fieldnames(T), fieldtypes(T))] # a::Int, b::String, ...
    quote
        StructTypes.StructType(::Type{$(esc(struct_type))}) = StructTypes.CustomStruct()
        StructTypes.lower(x::$(esc(struct_type))) = ($(esc(field_name)) = $field_value, $(x_names...))
        StructTypes.lowertype(::Type{$(esc(struct_type))}) = @NamedTuple{$field_name::$(esc(String)), $(name_types...)}
        $(esc(struct_type))(x::@NamedTuple{$field_name::$(esc(String)), $(name_types...)}) = $(esc(struct_type))($(x_names...))
    end
end

struct Foo
    x::Int
    y::String
end

@register_subtype Foo foo "bar"

e = Foo(1, "r")

StructTypes.StructType(Foo)
le = StructTypes.lower(e)
StructTypes.lowertype(M.NewEventName)

Foo(le)

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

Successfully merging a pull request may close this issue.

1 participant