Skip to content

Commit

Permalink
Refactor disambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Nov 23, 2023
1 parent 0f6dd0c commit d312e31
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
22 changes: 9 additions & 13 deletions src/disambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,29 @@ 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))
end
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
Expand Down
4 changes: 2 additions & 2 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d312e31

Please sign in to comment.