From 9321b515f20775c62947ea3c375ba65abd2768f7 Mon Sep 17 00:00:00 2001 From: Tortar Date: Thu, 25 Jan 2024 20:39:50 +0100 Subject: [PATCH] Implement constructor function --- src/CompactStructTypes.jl | 9 +++++++++ src/MixedStructTypes.jl | 2 +- src/SumStructTypes.jl | 14 ++++++++++++++ test/runtests.jl | 2 ++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/CompactStructTypes.jl b/src/CompactStructTypes.jl index 2e69398..1d93153 100644 --- a/src/CompactStructTypes.jl +++ b/src/CompactStructTypes.jl @@ -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], ", ") @@ -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 diff --git a/src/MixedStructTypes.jl b/src/MixedStructTypes.jl index 09774b6..8793689 100644 --- a/src/MixedStructTypes.jl +++ b/src/MixedStructTypes.jl @@ -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") diff --git a/src/SumStructTypes.jl b/src/SumStructTypes.jl index 850248a..a39ba4c 100644 --- a/src/SumStructTypes.jl +++ b/src/SumStructTypes.jl @@ -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) @@ -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...) diff --git a/test/runtests.jl b/test/runtests.jl index e6932c0..6aae5c8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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)