Skip to content

Commit

Permalink
Allow Abstract Type definitions (#21)
Browse files Browse the repository at this point in the history
* Allow Abstract Type definitions

* Update ProtoStruct.jl

* Update test_ProtoStruct.jl

* Update test_ProtoStruct.jl

* Use any

* Update ProtoStruct.jl

* Update test_ProtoStruct.jl
  • Loading branch information
Tortar authored Oct 13, 2023
1 parent 32acc26 commit 2ea8b9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/ProtoStruct.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

macro proto( expr )
if expr.head == :macrocall && expr.args[1] == Symbol("@kwdef")
expr = expr.args[3]
Expand All @@ -9,6 +10,14 @@ macro proto( expr )
ismutable = expr.args[1]

name = expr.args[2]

if !(name isa Symbol) && name.head == :<:
abstract_type = name.args[2]
name = name.args[1]
else
abstract_type = :(Any)
end

type_parameters = nothing
type_parameter_names = []
type_parameter_types = []
Expand Down Expand Up @@ -83,7 +92,7 @@ macro proto( expr )
ex = if ismutable
quote
if !@isdefined $name
struct $name{NT<:NamedTuple}
struct $name{NT<:NamedTuple} <: $abstract_type
properties::NT
end # struct
else
Expand Down Expand Up @@ -119,7 +128,7 @@ macro proto( expr )
else
quote
if !@isdefined $name
struct $name{NT<:NamedTuple}
struct $name{NT<:NamedTuple} <: $abstract_type
properties::NT
end # struct
else
Expand Down
7 changes: 5 additions & 2 deletions test/test_ProtoStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ end
@test_throws MethodError tm.F = "2"
@test propertynames(tm) == (:F, :G)
end

@proto mutable struct TestParametricMutation{T, V <: Real}

abstract type AbstractMutation end

@proto mutable struct TestParametricMutation{T, V <: Real} <: AbstractMutation
A::Int = 1
B = :no
C::T = nothing
Expand All @@ -100,4 +102,5 @@ end
@test tpm.C === nothing
@test tpm.D == 1.2
@test tpm.E == "nope"
@test TestParametricMutation <: AbstractMutation
end

0 comments on commit 2ea8b9d

Please sign in to comment.