# Unitful: how to restrict units to specific dimension

**URL:** https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295
**Category:** General Usage
**Tags:** question
**Created:** [June 30, 2020, 10:02am UTC](https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295 "2020-06-30T10:02:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)
#### Post date: [June 30, 2020, 10:02am UTC](https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295/1 "2020-06-30T10:02:54Z")

</div>

Hello everyone,  
My question is very similar to [How To Properly Use Unitful](https://discourse.julialang.org/t/how-to-properly-use-unitful/40295).

I would like to make a function that is similar to uconvert in that its first argument is a unit of dimension length:

```julia
f1(unit :: ??) = unit

```

What type signature should I put instead of ?? to get the following outcome?

```julia
julia> f1(u"m")
m
julia> f1(u"mm")
mm
julia> f1(u"s")
ERROR:...

```

I can do something very close with

```julia
f2(value::T) where {T<:Unitful.Length} = value

```

But then I have to put a value in front:

```julia
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
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

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [June 30, 2020, 11:16am UTC](https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295/2 "2020-06-30T11:16:22Z")

</div>

```julia
f1(unit::Unitful.LengthUnits) = unit

```

---

<div class="post-metadata">

### Author: ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)
#### Post date: [June 30, 2020, 11:29am UTC](https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295/3 "2020-06-30T11:29:40Z")

</div>

Perfect!  
Thanks!

Olivier
