Skip to content

Commit 8c24478

Browse files
committed
Clean up conversion to numbers
1 parent 2694747 commit 8c24478

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/disambiguities.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ 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
5062
for type in (Signed, Float64, Float32, Rational), op in (:flipsign, :copysign)
5163
@eval function Base.$(op)(x::$type, y::AbstractRealQuantity)
5264
return $(op)(x, ustrip(y))

src/utils.jl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ end
2525
return output
2626
end
2727

28-
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
29-
@eval Base.convert(::Type{$base_type}, q::$type) = q
30-
end
31-
function Base.convert(::Type{T}, q::UnionAbstractQuantity) where {T<:Number}
32-
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
33-
return convert(T, ustrip(q))
34-
end
3528
function Base.promote_rule(::Type{Dimensions{R1}}, ::Type{Dimensions{R2}}) where {R1,R2}
3629
return Dimensions{promote_type(R1,R2)}
3730
end
@@ -110,6 +103,21 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES
110103
end
111104
end
112105

106+
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
107+
@eval begin
108+
function (::Type{T})(q::$type) where {T<:Number}
109+
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
110+
return convert(T, ustrip(q))
111+
end
112+
function Base.convert(::Type{T}, q::$type) where {T<:Number}
113+
return T(q)
114+
end
115+
function Base.convert(::Type{$base_type}, q::$type)
116+
return q
117+
end
118+
end
119+
end
120+
113121
"""
114122
promote_except_value(q1::UnionAbstractQuantity, q2::UnionAbstractQuantity)
115123
@@ -298,9 +306,13 @@ tryrationalize(::Type{R}, x) where {R} = isinteger(x) ? convert(R, round(Int, x)
298306
Base.showerror(io::IO, e::DimensionError) = print(io, "DimensionError: ", e.q1, " and ", e.q2, " have incompatible dimensions")
299307
Base.showerror(io::IO, e::DimensionError{<:Any,Nothing}) = print(io, "DimensionError: ", e.q1, " is not dimensionless")
300308

301-
Base.convert(::Type{Q}, q::UnionAbstractQuantity) where {Q<:UnionAbstractQuantity} = q
302-
Base.convert(::Type{Q}, q::UnionAbstractQuantity) where {T,Q<:UnionAbstractQuantity{T}} = new_quantity(Q, convert(T, ustrip(q)), dimension(q))
303-
Base.convert(::Type{Q}, q::UnionAbstractQuantity) where {T,D,Q<:UnionAbstractQuantity{T,D}} = new_quantity(Q, convert(T, ustrip(q)), convert(D, dimension(q)))
309+
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
310+
@eval begin
311+
Base.convert(::Type{Q}, q::$type) where {Q<:UnionAbstractQuantity} = q
312+
Base.convert(::Type{Q}, q::$type) where {T,Q<:UnionAbstractQuantity{T}} = new_quantity(Q, convert(T, ustrip(q)), dimension(q))
313+
Base.convert(::Type{Q}, q::$type) where {T,D,Q<:UnionAbstractQuantity{T,D}} = new_quantity(Q, convert(T, ustrip(q)), convert(D, dimension(q)))
314+
end
315+
end
304316

305317
Base.convert(::Type{D}, d::AbstractDimensions) where {D<:AbstractDimensions} = d
306318
Base.convert(::Type{D}, d::AbstractDimensions) where {R,D<:AbstractDimensions{R}} = D(d)

test/unittests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,9 +1258,9 @@ end
12581258
functions = (
12591259
:float, :abs, :real, :imag, :conj, :adjoint, :unsigned,
12601260
:nextfloat, :prevfloat, :identity, :transpose,
1261-
:copysign, :flipsign, :mod, :modf,
1261+
:copysign, :flipsign, :modf,
12621262
:floor, :trunc, :ceil, :significand,
1263-
:ldexp, :round,
1263+
:ldexp, :round, # :mod
12641264
)
12651265
for Q in (RealQuantity, Quantity, GenericQuantity), D in (Dimensions, SymbolicDimensions), f in functions
12661266
T = f in (:abs, :real, :imag, :conj) ? ComplexF64 : Float64
@@ -1274,7 +1274,7 @@ end
12741274
@eval @test $f($qx_dimensions)[$i] == $Q($f($x)[$i], $dim)
12751275
end
12761276
end
1277-
elseif f in (:copysign, :flipsign, :rem, :mod) # Functions that need multiple inputs
1277+
elseif f in (:copysign, :flipsign, :rem) # Functions that need multiple inputs
12781278
for x in 5rand(T, 3) .- 2.5
12791279
for y in 5rand(T, 3) .- 2.5
12801280
dim = convert(D, dimension(u"m/s"))

0 commit comments

Comments
 (0)