# Unitful, dispatch on u"1/s" and u"rad/s"

**URL:** <https://discourse.julialang.org/t/unitful-dispatch-on-u-1-s-and-u-rad-s/42851>\
**Category:** General Usage\
**Created:** [July 10, 2020, 3:31pm UTC](https://discourse.julialang.org/t/unitful-dispatch-on-u-1-s-and-u-rad-s/42851 "2020-07-10T15:31:48Z")\
**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:** [July 10, 2020, 3:31pm UTC](https://discourse.julialang.org/t/unitful-dispatch-on-u-1-s-and-u-rad-s/42851/1 "2020-07-10T15:31:48Z")

</div>

Hello everyone,

This is a follow up question from [this topic](https://discourse.julialang.org/t/unitful-how-to-restrict-units-to-specific-dimension/42295).

Now instead of just the unit I would like to have different quantities. Let me explain.  
I want to dispatch on units of 1/s and rad/s.

Now I do the following:

```julia
my_units(quantity :: typeof(1.0u"1/s")) = "only works for real value of Hz"
my_units(quantity :: typeof(1.0u"rad/s")) = "only works for real value of radHz"

```

This works only for real values.  
My question is how can I make this work for any type, like ints or rational.  
I know I can do the following:

```julia
my_units(quantity :: Unitful.Frequency) = "works for real and integer values of Hz"

```

The problem is that if I give radHz, I will get the same result. How can define a method that dispatches on radHz for any type of the value?

Thanks in advance,  
Olivier

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [July 10, 2020, 4:44pm UTC](https://discourse.julialang.org/t/unitful-dispatch-on-u-1-s-and-u-rad-s/42851/2 "2020-07-10T16:44:22Z")

</div>

It’s a bit tricky, but all the information is there: [Manipulating units - Unitful.jl](https://painterqubits.github.io/Unitful.jl/latest/manipulations/)

You can see that the information is encoded in a type `Quantity{T,D,U}`:

```julia
julia> typeof(1.0u"1/s")
Quantity{Float64,𝐓^-1,Unitful.FreeUnits{(s^-1,),𝐓^-1,nothing}}

julia> typeof(1.0u"rad/s")
Quantity{Float64,𝐓^-1,Unitful.FreeUnits{(rad, s^-1),𝐓^-1,nothing}}

```

`T` is the number type, and `D` and `U` are dimensions and units, respectively.

It is enough for your use to dispatch on the unit part only, keeping the type `T` and dimension `D` as free parameters. We use the `unit` function to select the part of the type we need.

```julia
my_units(q::Quantity{T,D,typeof(unit(1.0u"1/s"))}) where {T<:Real,D} = "only works for real value of Hz"
my_units(q::Quantity{T,D,typeof(unit(1.0u"rad/s"))}) where {T<:Real,D} = "only works for real value of radHz"

```

This now works as expected:

```julia
julia> my_units(1.0u"rad/s")
"only works for real value of radHz"

julia> my_units(1.0u"1/s")
"only works for real value of Hz"

```

---

<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:** [July 10, 2020, 5:02pm UTC](https://discourse.julialang.org/t/unitful-dispatch-on-u-1-s-and-u-rad-s/42851/3 "2020-07-10T17:02:33Z")

</div>

Great!  
Thanks a lot!  
I had tried something similar but I missed the key point

```julia
typeof(unit(1.0u"1/s"))

```

Thanks again!  
Olivier
