Skip to content

Commit

Permalink
More ambiguities found by Aqua
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Nov 21, 2023
1 parent d4cda51 commit 9424a22
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/disambiguities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,30 @@ for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign)
return $(op)(x, ustrip(y))
end
end

function Base.:*(l::Complex{Bool}, r::AbstractRealQuantity)
return new_quantity(typeof(r), l * ustrip(r), dimension(r))
end
function Base.:*(l::AbstractRealQuantity, r::Complex{Bool})
return new_quantity(typeof(l), ustrip(l) * r, dimension(l))
end

for op in (:(==), :isequal), base_type in (AbstractIrrational, AbstractFloat)
@eval begin
function Base.$(op)(l::AbstractRealQuantity, r::$base_type)
return $(op)(ustrip(l), r) && iszero(dimension(l))
end
function Base.$(op)(l::$base_type, r::AbstractRealQuantity)
return $(op)(l, ustrip(r)) && iszero(dimension(r))
end
end
end

function Base.isless(l::AbstractRealQuantity, r::AbstractFloat)
iszero(dimension(l)) || throw(DimensionError(l, r))
return isless(ustrip(l), r)
end
function Base.isless(l::AbstractFloat, r::AbstractRealQuantity)
iszero(dimension(r)) || throw(DimensionError(l, r))
return isless(l, ustrip(r))
end
21 changes: 19 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ end
# abstract number packages which may try to do the same thing.
# (which would lead to ambiguities)
const BASE_NUMERIC_TYPES = Union{
Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32,
Int8, UInt8, Int16, UInt16, Int32, UInt32,
Int64, UInt64, Int128, UInt128, Float16, Float32,
Float64, BigFloat, BigInt, ComplexF16, ComplexF32,
Float64, BigInt, ComplexF16, ComplexF32,
ComplexF64, Complex{BigFloat}, Rational{Int8}, Rational{UInt8},
Rational{Int16}, Rational{UInt16}, Rational{Int32}, Rational{UInt32},
Rational{Int64}, Rational{UInt64}, Rational{Int128}, Rational{UInt128},
Rational{BigInt},
}
# The following types require explicit promotion,
# as putting them in a union type creates different ambiguities
const AMBIGUOUS_NUMERIC_TYPES = (Bool, BigFloat)

for (type, _, _) in ABSTRACT_QUANTITY_TYPES
@eval begin
function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
Expand All @@ -88,6 +92,19 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
end
end
for numeric_type in AMBIGUOUS_NUMERIC_TYPES
@eval begin
function Base.convert(::Type{Q}, x::$numeric_type) where {T,D,Q<:$type{T,D}}
return new_quantity(Q, convert(T, x), D())
end
function Base.promote_rule(::Type{Q}, ::Type{$numeric_type}) where {T,D,Q<:$type{T,D}}
return with_type_parameters(promote_quantity(Q, $numeric_type), promote_type(T, $numeric_type), D)
end
function Base.promote_rule(::Type{$numeric_type}, ::Type{Q}) where {T,D,Q<:$type{T,D}}
return with_type_parameters(promote_quantity(Q, $numeric_type), promote_type(T, $numeric_type), D)
end
end
end
end

"""
Expand Down

0 comments on commit 9424a22

Please sign in to comment.