From d3966e8e123cb750d9e320614863c71efb156821 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Tue, 13 Feb 2024 10:55:02 +0100 Subject: [PATCH] Fix creation of SumTypes in untyped vector (#75) * Fix creation of SumTypes in untyped vector * Update runtests.jl * actually it works fine * Update runtests.jl * Update runtests.jl * Update runtests.jl * Update runtests.jl * add another test --- src/sum_type.jl | 1 - test/runtests.jl | 20 +++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/sum_type.jl b/src/sum_type.jl index 1910a77..faaf9cd 100644 --- a/src/sum_type.jl +++ b/src/sum_type.jl @@ -199,7 +199,6 @@ function generate_constructor_exprs(T_name, T_params, T_params_constrained, T_na end if true push!(converts, T_uninit => quote - $Base.convert(::$Type{$T_init}, $x::$T_uninit) where {$(T_params_constrained...)} = $T_init($unwrap($x)) $Base.convert(::$Type{<:$T_init}, $x::$T_uninit) where {$(T_params_constrained...)} = $T_init($unwrap($x)) (::$Type{<:$T_init})($x::$T_uninit) where {$(T_params_constrained...)} = $T_init($unwrap($x)) (::$Type{$T_init})($x::$T_uninit) where {$(T_params_constrained...)} = $T_init($unwrap($x)) diff --git a/test/runtests.jl b/test/runtests.jl index be59fb0..10baacb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -391,6 +391,24 @@ using Test, SumTypes true end end -end +end + +end + +#------------------------------- + +@sum_type WT{X,Y} begin + W{X}(x::X) + T{Y}(y::Y) +end +@testset "Test creation inside vectors" begin + # https://github.com/MasonProtter/SumTypes.jl/issues/66 + @test [T(i) for i in 1:2] isa Vector{WT{Uninit, Int}} + @test [T(1)] isa Vector{WT{Uninit, Int}} + @test WT[T(1)] isa Vector{WT} + @test WT{Int}[W(1)] isa Vector{WT{Int}} + @test WT{Uninit,Int}[T(1)] isa Vector{WT{Uninit, Int}} + @test WT{Int,Int}[T(1), W(1)] isa Vector{WT{Int, Int}} + @test [T(1), W(1)] isa Vector{WT} end