Continuing the discussion from Unitful dispatch:
I want to dispatch specifically on u"inch"
. What am I missing?
In this MWE, I want mm(1)
, mm(1u"inch")
, and mm(1.0u"inch")
to all return 25.4 mm
.
using Unitful
InchType = Quantity{
<:Any,
Unitful.π,
Unitful.FreeUnits{(Unitful.inch,), Unitful.π, nothing},
}
function mm(x::Real)
x * u"inch" |> u"mm" |> float
end
function mm(x::InchType)
x |> u"mm" |> float
end
julia> x = 1u"inch"
1 inch
julia> XType = typeof(x)
Quantity{Int64, π , Unitful.FreeUnits{(inch,), π , nothing}}
julia> InchType
Quantity{<:Any, π , Unitful.FreeUnits{(inch,), π , nothing}}
julia> XType <: InchType
false
julia> mm(x)
ERROR: MethodError: no method matching mm(::Quantity{Int64, π, Unitful.FreeUnits{(inch,), π,
nothing}})
The function `mm` exists, but no method is defined for this combination of argument types.
Closest candidates are:
mm(::Quantity{<:Any, π, Unitful.FreeUnits{(inch,), π, nothing}} , ::Quantity{<:Any, π, Uni
tful.FreeUnits{(inch,), π, nothing}} )
@ Main C:\Users\nboyer.AIP\.julia\config\startup.jl:67
mm(::Real, ::Real)
@ Main C:\Users\nboyer.AIP\.julia\config\startup.jl:64
mm(::Tuple{Quantity{<:Any, π, Unitful.FreeUnits{(inch,), π, nothing}}, Quantity{<:Any, π,
Unitful.FreeUnits{(inch,), π, nothing}}} )
@ Main C:\Users\nboyer.AIP\.julia\config\startup.jl:61
...
Stacktrace:
[1] top-level scope
@ REPL[1]:1