Hello everyone,
My question is very similar to How To Properly Use Unitful.
I would like to make a function that is similar to uconvert in that its first argument is a unit of dimension length:
f1(unit :: ??) = unit
What type signature should I put instead of ?? to get the following outcome?
julia> f1(u"m")
m
julia> f1(u"mm")
mm
julia> f1(u"s")
ERROR:...
I can do something very close with
f2(value::T) where {T<:Unitful.Length} = value
But then I have to put a value in front:
julia> f2(1u"m")
1 m
julia> f2(1.0u"m")
1.0 m
julia> f2(1.0u"s")
ERROR: MethodError: no method matching f2(::Quantity{Float64,π,Unitful.FreeUnits{(s,),π,nothing}})
Thatβs nice, but then on the other hand
julia> f2(u"m")
ERROR: MethodError: no method matching f2(::Unitful.FreeUnits{(m,),π,nothing})
Closest candidates are:
f2(::T) where T<:(Union{Quantity{T,π,U}, Level{L,S,Quantity{T,π,U}} where S where L} where U where T) at REPL[31]:1
How can I get a more restrictive type signature to get what I want?
Many thanks in advance.
Olivier