# The death of optional arguments?

**URL:** <https://discourse.julialang.org/t/the-death-of-optional-arguments/7388>\
**Category:** Internals & Design\
**Created:** [November 29, 2017, 3:50pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388 "2017-11-29T15:50:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 3:50pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/1 "2017-11-29T15:50:38Z")

</div>

With NamedTuple keyword arguments and constant propagation pull requests both open, it sounds like keyword arguments will soon be type stable and fast. Does this spell the end for optional arguments? Keyword arguments have several advantages:

1. They can be used in any order (duh)
2. You can add new keywords easily with no breakage
3. Keywords help with readability

Disadvantages:

1. Slightly less verbose (tradeoff with readability)
2. Don’t participate in dispatch (but can be wrappers for functions which do)

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [November 29, 2017, 4:05pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/2 "2017-11-29T16:05:00Z")

</div>

I am really looking forward to those merges. I have been holding back on keyword arguments for performance reasons till now, which was unfortunate.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 29, 2017, 4:54pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/3 "2017-11-29T16:54:47Z")

</div>

Optional positional arguments are still quite useful. I’m not clear why we would get rid of such a basic and standard language feature.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:26pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/4 "2017-11-29T17:26:07Z")

</div>

What is an example of an interface where it would make more sense to use optional arguments than keyword arguments?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 29, 2017, 5:32pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/5 "2017-11-29T17:32:48Z")

</div>

> [@bramtayl](#):
>
> What is an example of an interface where it would make more sense to use positional arguments than keyword arguments?

Anything with a splat (`hcat`).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 29, 2017, 5:36pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/6 "2017-11-29T17:36:11Z")

</div>

> [@bramtayl](#):
>
> Does this spell the end for optional arguments?

I am not sure why you think this. Many languages have both just fine.

One, or at most two, optional arguments are kind of a sweet spot in most contexts when there is nothing else to parametrize. For 2+, it is usually better style to use keywords. There is place for both.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:40pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/7 "2017-11-29T17:40:49Z")

</div>

Oh sorry I meant what is an example where it makes more sense to use optional arguments than positional arguments. I’m totally fine with splats.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:43pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/8 "2017-11-29T17:43:17Z")

</div>

Which languages support both? Just out of curiosity

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 29, 2017, 5:47pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/9 "2017-11-29T17:47:59Z")

</div>

> [@bramtayl](#):
>
> Oh sorry I meant what is an example where it makes more sense to use optional arguments than positional arguments. I’m totally fine with splats.

Okay, the answer is still APIs where you splat some number of arguments. Those don’t need to be named by the user, you just need `N` of them in a row.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 29, 2017, 5:49pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/10 "2017-11-29T17:49:02Z")

</div>

Many. It is more or less the norm these days. See the intersection of

> **[Named parameters](https://rosettacode.org/wiki/Named_parameters)**
>
> Create a function which takes in a number of arguments which are specified by name rather than (necessarily) position, and show how to call the function. If the...

> **[Optional parameters](https://rosettacode.org/wiki/Optional_parameters)**
>
> Task Define a function/method/subroutine which sorts a sequence ("table") of sequences ("rows") of strings ("cells"), by one of the strings. Besides the input to...

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:50pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/11 "2017-11-29T17:50:25Z")

</div>

But you can splat named tuples as well, no?

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:55pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/12 "2017-11-29T17:55:16Z")

</div>

Ok yeah thats basically everyone

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:56pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/13 "2017-11-29T17:56:11Z")

</div>

Notably C# isn’t listed as having optional arguments

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 29, 2017, 5:57pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/14 "2017-11-29T17:57:14Z")

</div>

> [@bramtayl](#):
>
> But you can splat named tuples as well, no?

Why would you require a name for each one?

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 5:58pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/16 "2017-11-29T17:58:54Z")

</div>

Readability?  
Extensibility?  
Reorderability?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 29, 2017, 6:02pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/17 "2017-11-29T18:02:24Z")

</div>

> [@bramtayl](#):
>
> Readability?
> 
> Extensibility?
> 
> Reorderability?

No. `max(2,4,6,8)` (`max(x...)`) is objectively simpler, more readable, and more extendable than `max(x1=2,x2=4,x3=6,x4=8)` (`@generated function max(;kwargs...)`), and it’s easier to implement.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [November 29, 2017, 6:10pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/18 "2017-11-29T18:10:31Z")

</div>

In the packages I develop I also use optional arguments as fundamental parameters for an algorithm that are crucial for it. I use keyword more like things that fine-tune the algorithm.

For example, an initial condition would be optional argument since it can be automatically generated randomly but the outcome depends crucially on it. On the other hand, some kind of tolerance level is a keyword argument.

I like it very much this way, and it allows me to keep this behaviour choice consistent across functions. So I am happy that I can use both arguments and keyword arguments.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 6:23pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/19 "2017-11-29T18:23:22Z")

</div>

Maybe I’m still being dense. It looks like max is defined as

```julia
($op)(a, b, c, xs...) = afoldl($op, ($op)(($op)(a,b),c), xs...)

```

Those aren’t the kind of optional arguments I’m thinking of. I mean optional arguments in the sense of

```julia
join(strings, delim = "", last = "")

```

Which I think would make more sense as

```julia
join(strings; delim = "", last = "")

```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [November 29, 2017, 6:24pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/20 "2017-11-29T18:24:53Z")

</div>

Why would you want your user to have to constantly use `rstrip(s, chars=mplah_mplah)` instead of just `rstrip(s, mplah mplah)`…? I would definitely not want this for functions that have less than 5 arguments

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 29, 2017, 6:28pm UTC](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388/21 "2017-11-29T18:28:37Z")

</div>

I guess I’m in general a fan of readable code. I think that `rstrip(s, chars=mplah_mplah)` is much more readable in case you forget what second argument is supposed to be (number of characters to split? A regex expression?). It is also much more flexible in terms of being able to go back and add other keyword arguments in any order without breaking code (I think?)

[Next page](https://discourse.julialang.org/t/the-death-of-optional-arguments/7388.md?page=2)
