From 23dd4c2dc44b77bc44267905109d92de18b6ef73 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Thu, 7 Nov 2024 15:40:18 +0100 Subject: [PATCH] better error message when the constructorof fallback is passed a Union --- src/ConstructionBase.jl | 3 +++ test/runtests.jl | 1 + 2 files changed, 4 insertions(+) diff --git a/src/ConstructionBase.jl b/src/ConstructionBase.jl index 4c4871a..c3a2ab7 100644 --- a/src/ConstructionBase.jl +++ b/src/ConstructionBase.jl @@ -27,6 +27,9 @@ for (name, path) in [ end @generated function constructorof(::Type{T}) where T + if T isa Union + throw(ArgumentError("`Union` not supported by the fallback method")) + end getfield(parentmodule(T), nameof(T)) end diff --git a/test/runtests.jl b/test/runtests.jl index 6e5fff9..23daa26 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -17,6 +17,7 @@ end @test constructorof(NamedTuple{(:a, :b)})(1.0, 2) === (a=1.0, b=2) @test constructorof(Tuple)(1.0, 2) === (1.0, 2) @test constructorof(Tuple{Nothing, Missing})(1.0, 2) === (1.0, 2) + @test_throws ArgumentError constructorof(Union{Bool, Int}) end @testset "getfields" begin