Skip to content

Commit 50e3f2d

Browse files
committed
Refactor disambiguities
1 parent 7999158 commit 50e3f2d

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

src/disambiguities.jl

+9-13
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,29 @@ end
4747
# Assorted calls found by Aqua: ################################################
4848
################################################################################
4949

50-
function Complex(q::AbstractRealQuantity)
51-
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
52-
return Complex(ustrip(q))
53-
end
54-
function Complex{T}(q::AbstractRealQuantity) where {T<:Real}
55-
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
56-
return Complex{T}(ustrip(q))
57-
end
58-
function Bool(q::AbstractRealQuantity)
59-
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
60-
return Bool(ustrip(q))
61-
end
6250
for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign)
6351
@eval function Base.$(op)(x::$type, y::AbstractRealQuantity)
6452
return $(op)(x, ustrip(y))
6553
end
6654
end
67-
for type in (Complex, Complex{Bool})
55+
for type in (:(Complex), :(Complex{Bool}))
6856
@eval begin
6957
function Base.:*(l::$type, r::AbstractRealQuantity)
7058
new_quantity(typeof(r), l * ustrip(r), dimension(r))
7159
end
7260
function Base.:*(l::AbstractRealQuantity, r::$type)
7361
new_quantity(typeof(l), ustrip(l) * r, dimension(l))
7462
end
63+
function $type(q::AbstractRealQuantity)
64+
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
65+
return $type(ustrip(q))
66+
end
7567
end
7668
end
69+
function Bool(q::AbstractRealQuantity)
70+
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
71+
return Bool(ustrip(q))
72+
end
7773
function Base.:/(l::Complex, r::AbstractRealQuantity)
7874
new_quantity(typeof(r), l / ustrip(r), inv(dimension(r)))
7975
end

test/unittests.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ end
863863
x = Q(1.0u"1")
864864
@test Bool(x) == true
865865
@test Bool(ustrip(x)) == true
866-
@test Bool(Q(0.0u"m")) == false
867-
@test Bool(ustrip(Q(0.0u"m"))) == false
866+
@test Bool(Q(0.0u"1")) == false
867+
@test Bool(ustrip(Q(0.0u"1"))) == false
868868
x = Q(1.0u"m")
869869
@test_throws AssertionError Bool(x)
870870
end
@@ -1315,14 +1315,14 @@ end
13151315
# Also do test without dimensions (need dimensionless)
13161316
qx_dimensionless = Q(x, D)
13171317
qy_dimensionless = Q(y, D)
1318-
@eval @test $f($x, $qy_dimensionless) == $Q($f($x, $y), $D)
1319-
@eval @test $f($qx_dimensionless, $y) == $Q($f($x, $y), $D)
1318+
@eval @test $f($x, $qy_dimensionless) $Q($f($x, $y), $D)
1319+
@eval @test $f($qx_dimensionless, $y) $Q($f($x, $y), $D)
13201320
@eval @test_throws DimensionError $f($qx_dimensions, $y)
13211321
@eval @test_throws DimensionError $f($x, $qy_dimensions)
13221322
if f == :rem
13231323
# Can also do other rounding modes
13241324
for r in (:RoundFromZero, :RoundNearest, :RoundUp, :RoundDown)
1325-
@eval @test $f($qx_dimensions, $qy_dimensions, $r) == $Q($f($x, $y, $r), $dim)
1325+
@eval @test $f($qx_dimensions, $qy_dimensions, $r) $Q($f($x, $y, $r), $dim)
13261326
end
13271327
end
13281328
end

0 commit comments

Comments
 (0)