|
1 |
| -Base.isless(::AbstractQuantity, ::Missing) = missing |
2 |
| -Base.isless(::Missing, ::AbstractQuantity) = missing |
3 |
| -Base.:(==)(::AbstractQuantity, ::Missing) = missing |
4 |
| -Base.:(==)(::Missing, ::AbstractQuantity) = missing |
5 |
| -Base.isapprox(::AbstractQuantity, ::Missing; kws...) = missing |
6 |
| -Base.isapprox(::Missing, ::AbstractQuantity; kws...) = missing |
| 1 | +for op in (:isless, :(==), :isequal, :(<)), (type, _, _) in ABSTRACT_QUANTITY_TYPES |
| 2 | + @eval begin |
| 3 | + Base.$(op)(::$type, ::Missing) = missing |
| 4 | + Base.$(op)(::Missing, ::$type) = missing |
| 5 | + end |
| 6 | +end |
| 7 | +for op in (:isapprox,), (type, _, _) in ABSTRACT_QUANTITY_TYPES |
| 8 | + @eval begin |
| 9 | + Base.$(op)(::$type, ::Missing; kws...) = missing |
| 10 | + Base.$(op)(::Missing, ::$type; kws...) = missing |
| 11 | + end |
| 12 | +end |
7 | 13 |
|
8 |
| -Base.:(==)(::AbstractQuantity, ::WeakRef) = error("Cannot compare a quantity to a weakref") |
9 |
| -Base.:(==)(::WeakRef, ::AbstractQuantity) = error("Cannot compare a weakref to a quantity") |
| 14 | +for (type, _, _) in ABSTRACT_QUANTITY_TYPES |
| 15 | + @eval begin |
| 16 | + Base.:(==)(::$type, ::WeakRef) = error("Cannot compare a quantity to a weakref") |
| 17 | + Base.:(==)(::WeakRef, ::$type) = error("Cannot compare a weakref to a quantity") |
| 18 | + end |
| 19 | +end |
10 | 20 |
|
11 | 21 | Base.:*(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
|
12 | 22 | Base.:*(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for multiplication. You used multiplication on types: $(typeof(l)) and $(typeof(r)).")
|
13 | 23 | Base.:/(l::AbstractDimensions, r::Number) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
|
14 | 24 | Base.:/(l::Number, r::AbstractDimensions) = error("Please use an `UnionAbstractQuantity` for division. You used division on types: $(typeof(l)) and $(typeof(r)).")
|
| 25 | + |
| 26 | +# Promotion ambiguities |
| 27 | +function Base.promote_rule(::Type{F}, ::Type{Bool}) where {F<:FixedRational} |
| 28 | + return F |
| 29 | +end |
| 30 | +function Base.promote_rule(::Type{Bool}, ::Type{F}) where {F<:FixedRational} |
| 31 | + return F |
| 32 | +end |
| 33 | +function Base.promote_rule(::Type{F}, ::Type{BigFloat}) where {F<:FixedRational} |
| 34 | + return promote_type(Rational{eltype(F)}, BigFloat) |
| 35 | +end |
| 36 | +function Base.promote_rule(::Type{BigFloat}, ::Type{F}) where {F<:FixedRational} |
| 37 | + return promote_type(Rational{eltype(F)}, BigFloat) |
| 38 | +end |
| 39 | +function Base.promote_rule(::Type{F}, ::Type{T}) where {F<:FixedRational,T<:AbstractIrrational} |
| 40 | + return promote_type(Rational{eltype(F)}, T) |
| 41 | +end |
| 42 | +function Base.promote_rule(::Type{T}, ::Type{F}) where {F<:FixedRational,T<:AbstractIrrational} |
| 43 | + return promote_type(Rational{eltype(F)}, T) |
| 44 | +end |
| 45 | + |
| 46 | +################################################################################ |
| 47 | +# Assorted calls found by Aqua: ################################################ |
| 48 | +################################################################################ |
| 49 | + |
| 50 | +for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign) |
| 51 | + @eval function Base.$(op)(x::$type, y::AbstractRealQuantity) |
| 52 | + return $(op)(x, ustrip(y)) |
| 53 | + end |
| 54 | +end |
| 55 | +for type in (:(Complex), :(Complex{Bool})) |
| 56 | + @eval begin |
| 57 | + function Base.:*(l::$type, r::AbstractRealQuantity) |
| 58 | + new_quantity(typeof(r), l * ustrip(r), dimension(r)) |
| 59 | + end |
| 60 | + function Base.:*(l::AbstractRealQuantity, r::$type) |
| 61 | + new_quantity(typeof(l), ustrip(l) * r, dimension(l)) |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | +function Complex{T}(q::AbstractRealQuantity) where {T<:Real} |
| 66 | + @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." |
| 67 | + return Complex{T}(ustrip(q)) |
| 68 | +end |
| 69 | +for type in (:Bool, :Complex) |
| 70 | + @eval function $type(q::AbstractRealQuantity) |
| 71 | + @assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead." |
| 72 | + return $type(ustrip(q)) |
| 73 | + end |
| 74 | +end |
| 75 | +function Base.:/(l::Complex, r::AbstractRealQuantity) |
| 76 | + new_quantity(typeof(r), l / ustrip(r), inv(dimension(r))) |
| 77 | +end |
| 78 | +function Base.:/(l::AbstractRealQuantity, r::Complex) |
| 79 | + new_quantity(typeof(l), ustrip(l) / r, dimension(l)) |
| 80 | +end |
| 81 | +for op in (:(==), :isequal), base_type in (AbstractIrrational, AbstractFloat) |
| 82 | + @eval begin |
| 83 | + function Base.$(op)(l::AbstractRealQuantity, r::$base_type) |
| 84 | + return $(op)(ustrip(l), r) && iszero(dimension(l)) |
| 85 | + end |
| 86 | + function Base.$(op)(l::$base_type, r::AbstractRealQuantity) |
| 87 | + return $(op)(l, ustrip(r)) && iszero(dimension(r)) |
| 88 | + end |
| 89 | + end |
| 90 | +end |
| 91 | +function Base.isless(l::AbstractRealQuantity, r::AbstractFloat) |
| 92 | + iszero(dimension(l)) || throw(DimensionError(l, r)) |
| 93 | + return isless(ustrip(l), r) |
| 94 | +end |
| 95 | +function Base.isless(l::AbstractFloat, r::AbstractRealQuantity) |
| 96 | + iszero(dimension(r)) || throw(DimensionError(l, r)) |
| 97 | + return isless(l, ustrip(r)) |
| 98 | +end |
| 99 | +for (type, _, _) in ABSTRACT_QUANTITY_TYPES, numeric_type in (Bool, BigFloat) |
| 100 | + @eval begin |
| 101 | + function Base.promote_rule(::Type{Q}, ::Type{$numeric_type}) where {T,D,Q<:$type{T,D}} |
| 102 | + return with_type_parameters(promote_quantity_on_value(Q, $numeric_type), promote_type(T, $numeric_type), D) |
| 103 | + end |
| 104 | + function Base.promote_rule(::Type{$numeric_type}, ::Type{Q}) where {T,D,Q<:$type{T,D}} |
| 105 | + return with_type_parameters(promote_quantity_on_value(Q, $numeric_type), promote_type(T, $numeric_type), D) |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments