# Using keyword arguments, default arguments, with and without Unitful

**URL:** <https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493>\
**Category:** General Usage\
**Created:** [October 16, 2020, 3:09pm UTC](https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493 "2020-10-16T15:09:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [October 16, 2020, 3:09pm UTC](https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493/1 "2020-10-16T15:09:37Z")

</div>

Hi, I have a complicated function which has enough arguments that I want to use keyword args, and I want to have some default values, and in the early stages I like to use Unitful to help me catch dimensional errors, but at other times I would like to be able to just use SI throughout and drop all the clutter of the units.

I can do that in the following way:

```julia
f_(a,b) = a+b # imagine a bit more complexity here!
f(a,b=1.0) = f_(a,b)
f(a::Quantity,b::Quantity = 1.0u"m") = f_(a,b)

```

and then `f(2.0e-3)` and `f(2.0u"mm")` both do what I would want.

I feel like there should be a way for multiple dispatch to solve this for me, but in my attempts I’ve only figured out new ways to generate a stack overflow.

[EDIT: simplified MWE]

---

<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:** [October 16, 2020, 4:52pm UTC](https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493/2 "2020-10-16T16:52:13Z")

</div>

you can use `Length` to make sure the quantities will be able to add up:

```julia
import Unitful: Length
f(a::Length, b::Length = 1.0m) = f_(a,b)

```

you can get rid of the units with `ustrip` (don’t forget to give the reference unit)

```julia
julia> ustrip(2mm)
2
julia> ustrip(m, 2mm)
1//500
julia> ustrip(Float64, m, 2mm)
0.002

```

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [October 16, 2020, 6:31pm UTC](https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493/3 "2020-10-16T18:31:51Z")

</div>

After noodling some more and being reminded that Julia’s multiple dispatch doesn’t allow multiple keyword-only methods of the same function, I think I’ll do the following for now:

```julia
f(a,b) = a+b # imagine a bit more complexity here!
f(;a=1.0,b=1.0) = f(a,b)
uf(;a::Quantity = 1.0u"m", b::Quantity = 1.0u"m") = f(a,b)

```

Edit: On the other hand, keyword args don’t play well with dot fusion which tips it towards my initial solution.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 16, 2020, 6:41pm UTC](https://discourse.julialang.org/t/using-keyword-arguments-default-arguments-with-and-without-unitful/48493/4 "2020-10-16T18:41:46Z")

</div>

> [@klaff](#):
>
> Julia’s multiple dispatch doesn’t allow multiple keyword-only methods of the same function

Keywors arguments don’t participate in Dispatch at all, that’s why only one set per function works.
