|
43 | 43 | function Base.promote_rule(::Type{<:Quantity{T1,D1}}, ::Type{<:Quantity{T2,D2}}) where {T1,T2,D1,D2}
|
44 | 44 | return Quantity{promote_type(T1,T2),promote_type(D1,D2)}
|
45 | 45 | end
|
| 46 | + |
| 47 | +# Define promotion rules for all basic numeric types, individually. |
| 48 | +# We don't want to define an opinionated promotion on <:Number, |
| 49 | +# or even <:AbstractFloat, as it could conflict with other |
| 50 | +# abstract number packages which may try to do the same thing. |
| 51 | +# (which would lead to ambiguities) |
| 52 | +const BASE_NUMERIC_TYPES = Union{ |
| 53 | + Bool, Int8, UInt8, Int16, UInt16, Int32, UInt32, |
| 54 | + Int64, UInt64, Int128, UInt128, Float16, Float32, |
| 55 | + Float64, BigFloat, BigInt, ComplexF16, ComplexF32, |
| 56 | + ComplexF64, Complex{BigFloat}, Rational{Int8}, Rational{UInt8}, |
| 57 | + Rational{Int16}, Rational{UInt16}, Rational{Int32}, Rational{UInt32}, |
| 58 | + Rational{Int64}, Rational{UInt64}, Rational{Int128}, Rational{UInt128}, |
| 59 | + Rational{BigInt}, |
| 60 | +} |
| 61 | +for (type, _, _) in ABSTRACT_QUANTITY_TYPES |
| 62 | + @eval function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES} |
| 63 | + return with_type_parameters(Q, promote_type(T, T2), D) |
| 64 | + end |
| 65 | + @eval function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}} |
| 66 | + return new_quantity(Q, convert(T, x), D()) |
| 67 | + end |
| 68 | +end |
46 | 69 | function Base.promote_rule(::Type{<:AbstractQuantity}, ::Type{<:Number})
|
47 | 70 | return Number
|
48 | 71 | end
|
|
0 commit comments