# Optional arguments+splatting

**URL:** <https://discourse.julialang.org/t/optional-arguments-splatting/44505>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 7, 2020, 3:22pm UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505 "2020-08-07T15:22:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 7, 2020, 3:22pm UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505/1 "2020-08-07T15:22:51Z")

</div>

Julia’s creators say they are greedy - so am I. I want to create a function g that accepts a list of arguments (I am trying with a named tuple), and passes it on to a function f. I have 3 requirements

- The user of g must provide “named arguments” as in “a=3”, for readability of the code calling g
- Arguments must be optional, with fallback on a default value
- and here comes the crunch: g must not be concerned with the details of this argument list.

Why would I ever want to do that? Well, in the real world, g does a bit of work behind the scenes.

I try

```julia
struct T
    a
    b
end
f(;a=0,b=0) = T(a,b)
g(foo,s...) = foo(s...)
r=g(f,(a=2))

```

but splatting was clearly not intended for named tuples. Any ideas that do not involve metaprogramming? 😀

I am knocking off for the day, but I am wondering if g could become a macro… metaprogramming-light.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 7, 2020, 3:47pm UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505/2 "2020-08-07T15:47:13Z")

</div>

> [@Philippe\_Maincon1](#):
>
> `foo(s...)`

Maybe you meant

```julia
foo(; s...) 

```

?

I can’t test this right now, BTW.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [August 7, 2020, 5:19pm UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505/3 "2020-08-07T17:19:43Z")

</div>

> [@Philippe\_Maincon1](#):
>
> ```julia
> struct T
> a
> b
> end
> f(;a=0,b=0) = T(a,b)
> g(foo,s...) = foo(s...)
> r=g(f,(a=2))
> 
> ```

Building up DFN’s answer, I think you want the following:

```julia
struct T
  a
  b
end
f(;a=0, b=0) = T(a ,b)
g(foo, s) = foo(;s...)
r = g(f,(a=2,))

```

Note that (a=2,) requires a comma because there is only 1 element. Otherwise, the parentheses are omitted. Also, I removed the splatting on s in g

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 7, 2020, 5:22pm UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505/4 "2020-08-07T17:22:47Z")

</div>

Thank you, that’s it!

---

<div class="post-metadata">

**Author:** ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Post date:** [August 8, 2020, 5:24am UTC](https://discourse.julialang.org/t/optional-arguments-splatting/44505/5 "2020-08-08T05:24:07Z")

</div>

Even sweeter:

```julia
struct T
  a
  b
end
f(;a=0, b=0) = T(a ,b)
g(foo; s...) = foo(;s...)
r = g(f,a=2) # no tuple syntax !

```

😁
