# Function with two unitful arguments with the same dimension

**URL:** https://discourse.julialang.org/t/function-with-two-unitful-arguments-with-the-same-dimension/55978
**Category:** General Usage
**Tags:** dispatch, function, unitful
**Created:** [February 25, 2021, 3:35am UTC](https://discourse.julialang.org/t/function-with-two-unitful-arguments-with-the-same-dimension/55978 "2021-02-25T03:35:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![OliverEvans96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliverevans96/32/4177_2.png) [@OliverEvans96](https://discourse.julialang.org/u/OliverEvans96)
#### Post date: [February 25, 2021, 3:35am UTC](https://discourse.julialang.org/t/function-with-two-unitful-arguments-with-the-same-dimension/55978/1 "2021-02-25T03:35:55Z")

</div>

Hello,

Using [Unitful.jl](https://github.com/PainterQubits/Unitful.jl), you can dispatch on dimensions or specific units. For example, you can write a method that takes any length quantity:

```julia
using Unitful: m, ft, Length

f(x::Length) = println("Got length: $x")

```

Or a function that takes only meters:

```julia
f(x::typeof(1m)) = println("Got meters: $x")

```

I could also write a function that takes two arguments with the same length unit

```julia
f(x::T, y::T) where T <: Length = println("Got same units: ($x, $y)")

```

But is there a way that I can write a function that requires that both of its arguments have the same dimension, but not necessarily the same unit?

---

<div class="post-metadata">

### Author: ![OliverEvans96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliverevans96/32/4177_2.png) [@OliverEvans96](https://discourse.julialang.org/u/OliverEvans96)
#### Post date: [February 25, 2021, 3:40am UTC](https://discourse.julialang.org/t/function-with-two-unitful-arguments-with-the-same-dimension/55978/2 "2021-02-25T03:40:01Z")

</div>

To answer my own question, I’ve found the following:

```julia
function g(x::AbstractQuantity{<:Number,D}, y::AbstractQuantity{<:Number,D}) where D
    println("same dims: $x + $y = $(x+y)")
end

```

which works as expected:

```julia
julia> g(1m, 2ft)
same dims: 1 m + 2 ft = 1006//625 m

```

I feel compelled to bound D, but I don’t think it’s actually possible, because `typeof(1m)` is `Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}`, and `𝐋` is a value, not a type. And if I recall, you can’t constrain values used as type parameters.

Also, I’m not sure if there’s a good reason to use `AbstractQuantity` over just `Quantity`. Are there other useful `AbstractQuantity`s out there?

If anyone else has another solution, I’d love to see it.

Thanks,  
Oliver
