# Unitful dispatch

**URL:** <https://discourse.julialang.org/t/unitful-dispatch/49480>\
**Category:** General Usage\
**Tags:** unitful, multidispatch\
**Created:** [November 2, 2020, 9:16pm UTC](https://discourse.julialang.org/t/unitful-dispatch/49480 "2020-11-02T21:16:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 2, 2020, 9:16pm UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/1 "2020-11-02T21:16:58Z")

</div>

Why this works:

```julia
julia> function hz(x::typeof(1.0u"Hz"))
       2x
       end
hz (generic function with 1 method)

```

and this does not work:

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

julia> function hz2(x::Quantity{Float64,𝐓^-1,Unitful.FreeUnits{(Hz,),𝐓^-1,nothing}})
       4x
       end
ERROR: UndefVarError: 𝐓 not defined
Stacktrace:
 [1] top-level scope at REPL[37]:1

```

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [November 2, 2020, 9:20pm UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/2 "2020-11-02T21:20:25Z")

</div>

Just guessing, but maybe you need `import Unitful: 𝐓`?

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 2, 2020, 9:23pm UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/3 "2020-11-02T21:23:34Z")

</div>

> [@ggggggggg](#):
>
> import Unitful: 𝐓

Yes,

```julia
import Unitful: 𝐓,Hz

```

solves the problem.  
Thanks!

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 2, 2020, 11:15pm UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/4 "2020-11-02T23:15:59Z")

</div>

A followup:

```julia
julia> function hz(x::typeof(1.0Hz))
       2x
       end
hz (generic function with 1 method)

julia> hz(2.0Hz)
4.0 Hz

julia> hz(1/0.5s)
ERROR: MethodError: no method matching hz(::Quantity{Float64,𝐓^-1,Unitful.FreeUnits{(s^-1,),𝐓^-1,nothing}})
Closest candidates are:
  hz(::Quantity{Float64,𝐓^-1,Unitful.FreeUnits{(Hz,),𝐓^-1,nothing}}) at REPL[68]:1
Stacktrace:
 [1] top-level scope at REPL[70]:1

```

However

```julia
julia> 2.0Hz + 1/0.5s 
4.0 s^-1

```

works?? Why?

How to define a type for dispatch that will accept either Hz or 1/s, Union?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 3, 2020, 12:14am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/5 "2020-11-03T00:14:02Z")

</div>

The real question is why do you have to do this? Its better if we can help you find a solution to your problem generally than specifically help on this one detail.

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 3, 2020, 12:32am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/6 "2020-11-03T00:32:23Z")

</div>

Sorry, I thought it is obvious. I want to write a function that (in this case) accepts frequency as an argument and dispatch using it. Frequency can be defined as Hertz or 1/s (an inverse of period). Computationally these units are identical. How to accomplish this? Maybe I have misunderstood the purpose of Unitful.

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [November 3, 2020, 12:37am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/7 "2020-11-03T00:37:59Z")

</div>

I haven’t used Unitful, but I’m guessing that `Hz` and `typeof(1/0.5s)` are different. In that case you have to define your function to accept a Union or define a wrapper to convert from inverse seconds to Hz. But because arithmetic operations are common, Unitful has evidently defined promotion rules to automatically convert types as needed in arithmetic operations.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 3, 2020, 12:39am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/8 "2020-11-03T00:39:49Z")

</div>

What I’m wondering is why do you want to do that! What is this for? Why dispatch on the units?

You will get more helpful answers if you add some context about the real world problem that dispatching on frequency provides a solution to.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 3, 2020, 2:37am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/9 "2020-11-03T02:37:16Z")

</div>

Just don’t specify the units you want, instead specify the dimensions. E.g. in this case, you want any `Quantity` which has dimensions of `𝐓^-1`, so you just do

```julia
julia> using Unitful; using Unitful: Hz, s, 𝐓

julia> f(x::Quantity{<:Any, 𝐓^-1}) = 2x
f (generic function with 1 method)

julia> f(2.0Hz)
4.0 Hz

julia> f(1/0.5s)
4.0 s^-1

```

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 3, 2020, 8:32am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/10 "2020-11-03T08:32:55Z")

</div>

I am writing an averaging function for interferometric data. It is possible to average over time or frequency and the algorithm is different for these → dispatch. BTW, I am using `DimensionalData.jl` for dealing with multidimensional data (in this case polarization, IF band, frequency channel and time), makes everything a lot easier, thanks!

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 3, 2020, 8:33am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/11 "2020-11-03T08:33:48Z")

</div>

This is exactly what I was looking for (and expands to many other cases), many thanks!

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [November 3, 2020, 9:04am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/12 "2020-11-03T09:04:48Z")

</div>

There are already build-in names for physical dimensions, you can find them [here](https://github.com/PainterQubits/Unitful.jl/blob/master/src/pkgdefaults.jl#L15-L51)

Use `Frequency`, it looks nicer and is more explicit

```julia
julia> using Unitful; using Unitful: s, Hz, Frequency

julia> f(x::Frequency) = 2x
f (generic function with 1 method)

julia> f(2Hz)
4 Hz

julia> f(1/2s)
1.0 s^-1

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [November 3, 2020, 9:40am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/13 "2020-11-03T09:40:08Z")

</div>

Ok dispatch like that absolutely makes sense then! So yes, as Mason pointed out is the way. I couldn’t tell if you were a newb trying to do something weird with units you don’t actually need to do 😉

Glad you’re finding DimensionalData.jl useful 🙂

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 3, 2020, 11:41am UTC](https://discourse.julialang.org/t/unitful-dispatch/49480/14 "2020-11-03T11:41:50Z")

</div>

Ah, yes. This is the most elegant solution, thanks, it is quite well hidden though 😉.
