# Keyword arguments in a clojure

**URL:** <https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860>\
**Category:** New to Julia\
**Created:** [May 12, 2018, 7:31pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860 "2018-05-12T19:31:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 12, 2018, 7:31pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/1 "2018-05-12T19:31:40Z")

</div>

How do you propogate keyword arguments?

```julia
function f(x,y; z = 1)
return x + y + z
end

```

Now i want a function g which fixes `y` at `y = 1`, but still allows the user to vary the keyword argument `z`.

```julia
g(x; (keyword arguments here)) = f(x, 1; (keyword arguments here))

```

I would like g to inherit the default keyword arguments of f as well, such that you can use `g` exactly like `f` but with the middle argument, `y` fixed at 1.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 12, 2018, 7:40pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/2 "2018-05-12T19:40:15Z")

</div>

You can use the `...` splat notation for this. To catch keyword arguments, just put the splatted argument _after_ the `;`

```julia
julia> g(x; kw...) = f(x, 1; kw...)
g (generic function with 1 method)

julia> g(2)
4

julia> g(2, z=3)
6

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 12, 2018, 7:47pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/3 "2018-05-12T19:47:48Z")

</div>

Thanks, this works, but i am worried about performance. It looks like the splatting version is significantly slower.

```julia
function f(x, y; z = 1)
    x + y + z 
end

g(x; kwargs...) = f(x, 1; kwargs...)

function gTest(x, N)
    for i in 1:N
        g(1)
    end
end

function fTest(x, N)
    for i in 1:N
        f(1, 1)
    end
end

N = 1_000_000 

@time fTest(1, N)
@time gTest(1, N)

 > 0.005996 seconds (1.30 k allocations: 72.532 KiB)
 > 0.102350 seconds (1.00 M allocations: 76.513 MiB, 11.23% gc time)
    

```

edit: this is on 0.6.2

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 12, 2018, 8:13pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/4 "2018-05-12T20:13:27Z")

</div>

Keyword argument performance is hugely improved with Julia master (`v0.7-`), so this should improve in the future. Also, note that your benchmark is somewhat uninformative because your functions `gTest` and `fTest` reference a non-const global variable N. It’s much easier to just use BenchmarkTools.jl to get reliable results:

On Julia v0.6.2:

```julia
julia> using BenchmarkTools

julia> @btime f(1, 1)
  1.597 ns (0 allocations: 0 bytes)
3

julia> @btime g(1)
  20.791 ns (1 allocation: 80 bytes)
3

```

Interestingly, on Julia master, it looks like both functions are completely optimized out:

Julia 0.7.0-DEV.5049:

```julia
julia> @btime f(1, 1)
  0.017 ns (0 allocations: 0 bytes)
3

julia> @btime g(1)
  0.017 ns (0 allocations: 0 bytes)
3

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 12, 2018, 8:17pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/5 "2018-05-12T20:17:55Z")

</div>

Great, thank you for the replies!

Yeah, I wonder if in 0.6.2, `g` was copying over the keyword argument in `f` or something. And now the compiler knows it doesn’t have to do that.

I really need to install 0.7.0 for these benchmarks.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 12, 2018, 8:20pm UTC](https://discourse.julialang.org/t/keyword-arguments-in-a-clojure/10860/6 "2018-05-12T20:20:42Z")

</div>

In v0.6 and earlier, keyword arguments were handled as dictionaries, so they inevitably allocated some memory and couldn’t be fully optimized out by the compiler. In v0.7 and above, they’re handled as named tuples (a new built-in type) which can be better optimized by the compiler. Here’s one of the relevant PRs: [https://github.com/JuliaLang/julia/pull/24795](https://github.com/JuliaLang/julia/pull/24795)
