Skip to content

Commit d032414

Browse files
authored
Resolve remaining method ambiguities (#626)
1 parent 0c53db8 commit d032414

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

src/logarithm.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,12 @@ function (Base.promote_rule(::Type{Level{L1,S1,T1}}, ::Type{Level{L2,S2,T2}})
277277
end
278278
end
279279

280-
function Base.promote_rule(::Type{Level{L,R,S}}, ::Type{Quantity{T,D,U}}) where {L,R,S,T,D,U}
281-
return promote_type(S, Quantity{T,D,U})
282-
end
283280
function Base.promote_rule(::Type{Quantity{T,D,U}}, ::Type{Level{L,R,S}}) where {L,R,S,T,D,U}
284281
return promote_type(S, Quantity{T,D,U})
285282
end
286283
function Base.promote_rule(::Type{Level{L,R,S}}, ::Type{T}) where {L,R,S,T<:Real}
287284
return promote_type(S,T)
288285
end
289-
function Base.promote_rule(::Type{T}, ::Type{Level{L,R,S}}) where {L,R,S,T<:Real}
290-
return promote_type(S,T)
291-
end
292286
function (Base.promote_rule(::Type{Gain{L1,S1,T1}}, ::Type{Gain{L2,S2,T2}})
293287
where {L1,L2,S1,S2,T1,T2})
294288
if L1 == L2

src/promotion.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ promote_unit(x::Units, y::Units, z::Units, t::Units...) =
2525
@inline _promote_unit(x::FreeUnits{N1,D}, y::FreeUnits{N2,D}) where {N1,N2,D} =
2626
upreferred(dimension(x))
2727

28-
@inline _promote_unit(x::T, y::T) where {T <: ContextUnits} = T() #ambiguity reasons
28+
@inline _promote_unit(x::ContextUnits{N,D,P,A}, y::ContextUnits{N,D,P,A}) where {N,D,P,A} = x #ambiguity reasons
2929
# same units, but promotion context disagrees
3030
@inline _promote_unit(x::ContextUnits{N,D,P1,A}, y::ContextUnits{N,D,P2,A}) where {N,D,P1,P2,A} =
3131
ContextUnits{N,D,promote_unit(P1(), P2()),A}()

src/quantities.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ function round(::Type{T}, x::AbstractQuantity, r::RoundingMode;
379379
unitless = ustrip(u, x)
380380
return Quantity{S, dimension(T), typeof(u)}(round(unitless, r; kwargs...))
381381
end
382+
round(::Type{T}, x::DimensionlessQuantity; kwargs...) where {S, T <: Quantity{S}} =
383+
invoke(round, Tuple{Type{T},AbstractQuantity}, T, x; kwargs...) # for ambiguity resolution
384+
round(::Type{T}, x::DimensionlessQuantity, r::RoundingMode; kwargs...) where {S, T <: Quantity{S}} =
385+
invoke(round, Tuple{Type{T},AbstractQuantity,RoundingMode}, T, x, r; kwargs...) # for ambiguity resolution
382386

383387
# that should actually be fixed in Base ↓
384388
for (f,r) = ((:trunc, :RoundToZero), (:floor, :RoundDown), (:ceil, :RoundUp))

src/types.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ struct Quantity{T,D,U} <: AbstractQuantity{T,D,U}
164164
end
165165

166166
# Field-only constructor
167-
Quantity{<:Any,D,U}(val) where {D,U} = Quantity{typeof(val),D,U}(val)
167+
Quantity{<:Any,D,U}(val::Number) where {D,U} = Quantity{typeof(val),D,U}(val)
168168

169169
constructorof(::Type{Unitful.Quantity{_,D,U}}) where {_,D,U} =
170170
Unitful.Quantity{T,D,U} where T
@@ -249,7 +249,7 @@ field, `val::T`, and the log of the ratio `val/S` is taken. This type differs fr
249249
"""
250250
struct Level{L, S, T<:RealOrRealQuantity} <: LogScaled{L}
251251
val::T
252-
function Level{L,S,T}(x) where {L,S,T}
252+
function Level{L,S,T}(x::Number) where {L,S,T}
253253
S isa ReferenceQuantity || throw(DomainError(S, "Reference quantity must be real."))
254254
dimension(S) != dimension(x) && throw(DimensionError(S,x))
255255
return new{L,S,T}(x)
@@ -266,8 +266,8 @@ For example, given a gain of `20dB`, we have `val===20`. This type differs from
266266
"""
267267
struct Gain{L, S, T<:Real} <: LogScaled{L}
268268
val::T
269+
Gain{L, S, T}(x::Number) where {L,S,T<:Real} = new{L,S,T}(x)
269270
end
270-
Unitful.Gain{L, S, T}(x::Unitful.Gain{L, S, T}) where {L,S,T<:Real} = x # for ambiguity resolution
271271

272272
"""
273273
struct MixedUnits{T<:LogScaled, U<:Units}

test/runtests.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,14 @@ end
10491049
@test round(typeof(1mm), 1.0314m) === 1031mm
10501050
@test round(typeof(1.0mm), 1.0314m) === 1031.0mm
10511051
@test round(typeof(1.0mm), 1.0314m; digits=1) === 1031.4mm
1052+
@test round(typeof(1.0°), 1.125°) === 1.0°
1053+
@test round(typeof(1.0°), 1.125°, RoundUp) === 2.0°
1054+
@test round(typeof(1.0°), 1.125°, digits=1) === 1.1°
1055+
@test round(typeof(1.0°), 1.125°, RoundUp, digits=1) === 1.2°
1056+
@test round(typeof(1.0°), 1rad) === 57.0°
1057+
@test round(typeof(1.0°), 1rad, RoundUp) === 58.0°
1058+
@test floor(typeof(1.0°), 1.125°) === 1.0°
1059+
@test floor(typeof(1.0°), 1.125°, digits=1) === 1.1°
10521060
@test round(u"inch", 1.0314m) === 41.0u"inch"
10531061
@test round(Int, u"inch", 1.0314m) === 41u"inch"
10541062
@test round(typeof(1m), 137cm) === 1m
@@ -2148,4 +2156,4 @@ end
21482156

21492157
using Aqua
21502158

2151-
Aqua.test_all(Unitful, ambiguities=false, unbound_args=false, piracy=VERSIONv"1.8")
2159+
Aqua.test_all(Unitful, ambiguities=VERSIONv"1.1", unbound_args=false, piracy=VERSIONv"1.8")

0 commit comments

Comments
 (0)