diff --git a/src/tangent.jl b/src/tangent.jl index a46dd031..75f5d258 100644 --- a/src/tangent.jl +++ b/src/tangent.jl @@ -90,9 +90,14 @@ struct ExplicitTangent{P <: Tuple} <: AbstractTangentSpace partials::P end + @eval struct TaylorTangent{C <: Tuple} <: AbstractTangentSpace coeffs::C - TaylorTangent(coeffs) = $(Expr(:new, :(TaylorTangent{typeof(coeffs)}), :coeffs)) + function TaylorTangent(coeffs) + bad_tangent_type = Union{DataType, Symbol, String, Nothing} # protect against obvious mistakes + any(c->isa(c, bad_tangent_type), coeffs) && throw(DomainError(coeffs, "Nonvector-space partial type")) + $(Expr(:new, :(TaylorTangent{typeof(coeffs)}), :coeffs)) + end end """ diff --git a/test/tangent.jl b/test/tangent.jl index 01a54607..d5ca2af0 100644 --- a/test/tangent.jl +++ b/test/tangent.jl @@ -1,4 +1,4 @@ -module tagent +module tangent using Diffractor using Diffractor: AbstractZeroBundle, ZeroBundle, DNEBundle using Diffractor: TaylorBundle, TaylorTangentIndex @@ -54,4 +54,12 @@ end @test truncate(et, Val(1)) == TaylorTangent((1.0,)) end + +@testset "Bad Partial Types" begin + @test_throws DomainError TaylorBundle{1}(1.5, (ZeroTangent,)) # mistakenly passing a type rather than a value + @test_throws DomainError TaylorBundle{1}(1.5, (:a,)) + @test_throws DomainError TaylorBundle{1}(1.5, (nothing,)) + @test_throws DomainError TaylorBundle{1}(1.5, ("x",)) +end + end # module