Unitful: how to restrict units to specific dimension

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

f1(unit::Unitful.LengthUnits) = unit
1 Like

Perfect!
Thanks!

Olivier