Skip to content

Commit

Permalink
Fix wrong constructors for compact_structs macro (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Feb 9, 2024
1 parent a80bc08 commit 38698c2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "MixedStructTypes"
uuid = "3d69f371-6fa5-5add-b11c-3293622cad62"
version = "0.2.0"
version = "0.2.1"

[deps]
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
Expand Down
53 changes: 27 additions & 26 deletions src/CompactStructs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,60 +86,60 @@ macro compact_structs(new_type, struct_defs)
f_inside_args = all_fields_n

conv_maybe = [x isa Symbol ? :() : x.args[2] for x in retrieve_fields_names(all_fields, true)]
f_inside_args = maybe_convert_fields(conv_maybe, f_inside_args, new_type_p, struct_spec_n)
f_inside_args2 = maybe_convert_fields(conv_maybe, f_inside_args, new_type_p, struct_spec_n; with_params=true)
f_inside_args = [f_inside_args..., Expr(:quote, type)]
f_inside_args2 = [f_inside_args2..., Expr(:quote, type)]
f_inside_args_no_t = maybe_convert_fields(conv_maybe, f_inside_args, new_type_p, struct_spec_n)
f_inside_args2_no_t = maybe_convert_fields(conv_maybe, f_inside_args, new_type_p, struct_spec_n; with_params=true)
f_inside_args = [f_inside_args_no_t..., Expr(:quote, type)]
f_inside_args2 = [f_inside_args2_no_t..., Expr(:quote, type)]

@capture(struct_t, struct_t_n_{struct_t_p__})
struct_t_p === nothing && (struct_t_p = [])
new_type_p = [t in struct_t_p ? t : (:(MixedStructTypes.Uninitialized))
for t in new_type_p]

expr_function_kwargs = :()
expr_function_kwargs2 = :()
expr_function_args = :()
expr_function_args2 = :()

if isempty(new_type_p)
expr_function_args = :(
function $(namify(struct_t))($(f_params_args...))
return $(namify(new_type))($(f_inside_args...))
end
)
end)
if is_kw
expr_function_kwargs = :(
function $(namify(struct_t))($f_params_kwargs)
return $(namify(new_type))($(f_inside_args...))
end
)
else
expr_function_kwargs = :()
end)
end
else
expr_function_args = :(
begin
function $(namify(struct_t))($(f_params_args_with_T...)) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args...))
end
function $(struct_t)($(f_params_args...)) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args2...))
end
end
)
end)
if !isempty(struct_t_p)
expr_function_args2 = :(function $(struct_t)($(f_params_args...)) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args2...))
end)
end
if is_kw
expr_function_kwargs = :(
begin
function $(namify(struct_t))($f_params_kwargs_with_T) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args...))
end
function $(namify(struct_t))($f_params_kwargs_with_T) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args...))
end)
if !isempty(struct_t_p)
expr_function_kwargs2 = :(
function $(struct_t)($f_params_kwargs) where {$(struct_t_p...)}
return $new_type_n{$(new_type_p...)}($(f_inside_args2...))
end
end
)
else
expr_function_kwargs = :()
end)
end
end
end

push!(expr_functions, expr_function_kwargs)
push!(expr_functions, expr_function_args)
push!(expr_functions, expr_function_kwargs2)
push!(expr_functions, expr_function_args2)
end

expr_kindof = :(MixedStructTypes.kindof(a::$(namify(new_type))) = getfield(a, $(Expr(:quote, gensym_type))))
Expand Down Expand Up @@ -242,6 +242,7 @@ end
function maybe_convert_fields(conv_maybe, f_inside_args, new_type_p, struct_spec_n; with_params=false)
f_inside_args_new = []
i = 1

for f in f_inside_args
if f in struct_spec_n
t = conv_maybe[i]
Expand Down
36 changes: 36 additions & 0 deletions test/compact_structs_macro_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ end
end
end

@compact_structs AA{T} begin
@kwdef mutable struct BB{T}
id::Int
a::T = 1
b::Int
c::Symbol
end
@kwdef mutable struct CC
id::Int
b::Int = 2
c::Symbol
d::Vector{Int}
end
@kwdef mutable struct DD{T}
id::Int
c::Symbol = :k
d::Vector{Int}
a::T
end
end

@testset "@compact_structs" begin

f = F((1,1), (1.0, 1.0), :s)
Expand Down Expand Up @@ -124,4 +145,19 @@ end
@test o1.y == 2.0 && o2.y == 3.0
@test o2.z == [1]
@test_throws "" o1.z


b1 = BB(1, 2, 1, :s)
c1 = CC(1, 1, :s, Int[])
d1 = DD(1, :s, [1], 1.0)
b2 = BB(; id = 1, b = 1, c = :s)
c2 = CC(; id = 1, c = :s, d = [1,2])
d2 = DD(; id = 1, d = [1], a = true)
b3 = BB{Float64}(1, 2, 1, :s)
d3 = DD{Float64}(1, :s, [1], 1.0)
b4 = BB{Int}(; id = 1, b = 1, c = :s)
d4 = DD{Int}(; id = 1, d = [1], a = true)

@test b3.a === 2.0 && d3.a === 1.0
@test b4.a === 1 && d4.a === 1
end
2 changes: 1 addition & 1 deletion test/sum_structs_macro_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ end
end

@static if VERSION >= v"1.10"
@testset "copy tests @struct_sum_type" begin
@testset "copy tests @sum_structs" begin
b = B((1,1), (1.0, 1.0), :s)
copy_b = copy(b)
@test copy_b.a == b.a
Expand Down

0 comments on commit 38698c2

Please sign in to comment.