# Canonical way to obtain \`NamedTuple\` from kwargs

**URL:** <https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075>\
**Category:** General Usage\
**Created:** [October 9, 2018, 2:38pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075 "2018-10-09T14:38:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 9, 2018, 2:38pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/1 "2018-10-09T14:38:41Z")

</div>

I kept forgetting to ask this. Keyword args are returned as a `Pairs` iterator. This cannot be converted to a `NamedTuple`. The simplest way I can see to obtain a `NamedTuple` from this would be `kwargs.data`. Presumably this is not, however, a “public” value.

Is there some better way of obtaining the `NamedTuple`? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [October 9, 2018, 3:09pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/2 "2018-10-09T15:09:23Z")

</div>

```nohighlight
julia> test(;kw_args...) = values(kw_args)
julia> test(a = 22) |> typeof
NamedTuple{(:a,),Tuple{Int64}}

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 9, 2018, 3:12pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/3 "2018-10-09T15:12:49Z")

</div>

Nice, thanks. This probably should get documentation somewhere [here](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments-1), hopefully I’ll remember to make a PR…

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 9, 2018, 8:13pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/4 "2018-10-09T20:13:26Z")

</div>

I wonder, why `kwargs` is not a `NamedTuple`, any insights?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 9, 2018, 8:14pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/5 "2018-10-09T20:14:30Z")

</div>

So you can iterate `symbol => value`.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 9, 2018, 8:43pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/6 "2018-10-09T20:43:06Z")

</div>

Conversely, is there a canonical way to splat a named tuple as keyword arguments?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 9, 2018, 8:47pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/7 "2018-10-09T20:47:02Z")

</div>

> [@GunnarFarneback](#):
>
> Conversely, is there a canonical way to splat a named tuple as keyword arguments?

As far as I know this is just, for example

```julia
f(;n...)

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 9, 2018, 8:50pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/8 "2018-10-09T20:50:51Z")

</div>

No, that splats as positional arguments.

```julia
ulia> k = (a = 1, b = 2)
(a = 1, b = 2)

julia> f(x, y) = [x, y]
f (generic function with 1 method)

julia> g(;x=0, y=0) = [x, y]
g (generic function with 1 method)

julia> f(k...)
2-element Array{Int64,1}:
 1
 2

julia> g(k...)
ERROR: MethodError: no method matching g(::Int64, ::Int64)
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 9, 2018, 8:51pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/9 "2018-10-09T20:51:41Z")

</div>

You’re missing a semi-colon `;`.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [October 9, 2018, 8:53pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/10 "2018-10-09T20:53:05Z")

</div>

Ah, right, thanks.

If you ever get to that PR, please include this direction too, even if it is obvious when you’re awake enough.

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [October 10, 2018, 5:24am UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/11 "2018-10-10T05:24:22Z")

</div>

How come they don’t execute with the same speed? (unrelated)

```julia
julia> using BenchmarkTools
julia> f(x,y) = x+y*x
f (generic function with 1 method)

julia> g(;x, y) = x+y*x
g (generic function with 1 method)

julia> k = (x=2,y=32)
(x = 2, y = 32)

julia> @btime f(k...)
  114.007 ns (1 allocation: 112 bytes)
66

julia> @btime g(;k...)
  64.446 ns (1 allocation: 32 bytes)
66

julia> @btime f($k...)
  120.523 ns (2 allocations: 144 bytes)
66

julia> @btime g(;$k...)
  1.899 ns (0 allocations: 0 bytes)
66
```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 10, 2018, 7:11pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/12 "2018-10-10T19:11:53Z")

</div>

> [@musm](#):
>
> ```julia
> julia> @btime g(;k...)
> 64.446 ns (1 allocation: 32 bytes) 66
> 
> ```

Is because `k` is a global variable.

Tuples have special handling for splatting in the compiler which doesn’t currently seem to extend to NamedTuples

```julia
julia> kk = (2, 32)
(2, 32)

julia> @btime f($kk...)
  0.025 ns (0 allocations: 0 bytes)
66

```

---

<div class="post-metadata">

**Author:** ![musm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/musm/32/3675_2.png) [@musm](https://discourse.julialang.org/u/musm)\
**Post date:** [October 10, 2018, 7:15pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/13 "2018-10-10T19:15:58Z")

</div>

doh yeah,

```julia
julia> const k = (x=2,y=32)
(x = 2, y = 32)

julia> f(x,y) = x+y*x
f (generic function with 1 method)

julia> g(;x, y) = x+y*x
g (generic function with 1 method)

julia> @btime f(k...)
  140.377 ns (1 allocation: 112 bytes)
66

julia> @btime g(;k...)
  2.399 ns (0 allocations: 0 bytes)
66

julia> @btime f($k...)
  158.462 ns (2 allocations: 144 bytes)
66

julia> @btime g(;$k...)
  2.599 ns (0 allocations: 0 bytes)
66

```

> Tuples have special handling for splatting in the compiler which doesn’t currently seem to extend to NamedTuples

is there an open issue for that?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 10, 2018, 7:17pm UTC](https://discourse.julialang.org/t/canonical-way-to-obtain-namedtuple-from-kwargs/16075/14 "2018-10-10T19:17:03Z")

</div>

> [@musm](#):
>
> is there an open issue for that?

I guess NamedTuple could be added to [Performance of splatting a number · Issue #29114 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29114).
