From d312e3124c8e8721e12e3d7494234180f8c1b085 Mon Sep 17 00:00:00 2001 From: MilesCranmer Date: Thu, 23 Nov 2023 17:40:52 +0000 Subject: [PATCH] Refactor disambiguities --- src/disambiguities.jl | 22 +++++++++------------- test/unittests.jl | 4 ++-- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/disambiguities.jl b/src/disambiguities.jl index 6eb75bd9..86f6a01c 100644 --- a/src/disambiguities.jl +++ b/src/disambiguities.jl @@ -47,24 +47,12 @@ end # Assorted calls found by Aqua: ################################################ ################################################################################ -function Complex(q::AbstractRealQuantity) - @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." - return Complex(ustrip(q)) -end -function Complex{T}(q::AbstractRealQuantity) where {T<:Real} - @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." - return Complex{T}(ustrip(q)) -end -function Bool(q::AbstractRealQuantity) - @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." - return Bool(ustrip(q)) -end for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign) @eval function Base.$(op)(x::$type, y::AbstractRealQuantity) return $(op)(x, ustrip(y)) end end -for type in (Complex, Complex{Bool}) +for type in (:(Complex), :(Complex{Bool})) @eval begin function Base.:*(l::$type, r::AbstractRealQuantity) new_quantity(typeof(r), l * ustrip(r), dimension(r)) @@ -72,8 +60,16 @@ for type in (Complex, Complex{Bool}) function Base.:*(l::AbstractRealQuantity, r::$type) new_quantity(typeof(l), ustrip(l) * r, dimension(l)) end + function $type(q::AbstractRealQuantity) + @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." + return $type(ustrip(q)) + end end end +function Bool(q::AbstractRealQuantity) + @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." + return Bool(ustrip(q)) +end function Base.:/(l::Complex, r::AbstractRealQuantity) new_quantity(typeof(r), l / ustrip(r), inv(dimension(r))) end diff --git a/test/unittests.jl b/test/unittests.jl index b159f3b9..185b0d37 100644 --- a/test/unittests.jl +++ b/test/unittests.jl @@ -863,8 +863,8 @@ end x = Q(1.0u"1") @test Bool(x) == true @test Bool(ustrip(x)) == true - @test Bool(Q(0.0u"m")) == false - @test Bool(ustrip(Q(0.0u"m"))) == false + @test Bool(Q(0.0u"1")) == false + @test Bool(ustrip(Q(0.0u"1"))) == false x = Q(1.0u"m") @test_throws AssertionError Bool(x) end