Skip to content

Commit

Permalink
Implement constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Jan 25, 2024
1 parent 0ade685 commit 9321b51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/CompactStructTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ macro compact_struct_type(new_type, struct_defs = nothing)

expr_kindof = :(MixedStructTypes.kindof(a::$(namify(new_type))) = getfield(a, $(Expr(:quote, gensym_type))))

branching_constructor = generate_branching_types(namify.(types_each), [:(return $v) for v in namify.(types_each)])

expr_constructor = :(function MixedStructTypes.constructor(a::$(namify(new_type)))
kind = kindof(a)

$(branching_constructor...)
end)

expr_show = :(function Base.show(io::IO, a::$(namify(new_type)))
f_vals = [getfield(a, x) for x in fieldnames(typeof(a))[1:end-1] if getfield(a, x) != MixedStructTypes.uninit]
vals = join([MixedStructTypes.print_transform(x) for x in f_vals], ", ")
Expand Down Expand Up @@ -173,6 +181,7 @@ macro compact_struct_type(new_type, struct_defs = nothing)
$(expr_setprop)
$(expr_propnames)
$(expr_copy)
$(expr_constructor)
$(expr_show)
nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/MixedStructTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export @compact_struct_type
export kindof

function kindof end
function copy_spec end
function constructor end

include("SumStructTypes.jl")
include("CompactStructTypes.jl")
Expand Down
14 changes: 14 additions & 0 deletions src/SumStructTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ macro sum_struct_type(type, struct_defs = nothing)
$(branching_kindof...)
end)

branching_constructor = generate_branching_variants(variants_types_names, [:(return $v) for v in variants_types_names])

expr_constructor = :(function MixedStructTypes.constructor(a::$(namify(type)))
type_a = (typeof)(a)
MixedStructTypes.SumTypes.check_sum_type(type_a)
MixedStructTypes.SumTypes.assert_exhaustive(Val{(MixedStructTypes.SumTypes.tags)(type_a)},
Val{$(Tuple(variants_types_names))})

data_a = (MixedStructTypes.SumTypes.unwrap)(a)

$(branching_constructor...)
end)

fields_each_symbol = [:(return $(Tuple(f))) for f in retrieve_fields_names.(fields_each, false)]
branching_propnames = generate_branching_variants(variants_types_names, fields_each_symbol)

Expand Down Expand Up @@ -188,6 +201,7 @@ macro sum_struct_type(type, struct_defs = nothing)
$(expr_kindof)
$(expr_propnames)
$(expr_copy)
$(expr_constructor)
$(expr_show)
$(expr_show_mime)
$(expr_constructors...)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ end
@test b.a == (3, 3)

@test kindof(b) == :B
@test MixedStructTypes.constructor(b) == B
@test propertynames(b) == (:a, :b, :c)

hawk_1 = Hawk(1.0, 2.0, 3)
Expand Down Expand Up @@ -154,6 +155,7 @@ end
@test f.a == (3, 3)

@test kindof(f) == :F
@test MixedStructTypes.constructor(f) == F
@test propertynames(f) == (:a, :b, :c)

copy_f = copy(f)
Expand Down

0 comments on commit 9321b51

Please sign in to comment.