# Rationale for kwargs as Iterators.Pairs

**URL:** <https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435>\
**Category:** Internals & Design\
**Tags:** kwargs\
**Created:** [August 15, 2021, 12:52pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435 "2021-08-15T12:52:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [August 15, 2021, 12:52pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435/1 "2021-08-15T12:52:31Z")

</div>

I’m curious why the keyword arguments are available wrapped in `Iterators.Pairs` in the function body:

```julia
function f(; kwargs...)
    # kwargs is a NamedTuple wrapped in an Iterators.Pairs
end

```

Is it legacy cruft from a time when named tuple were not so nice? Because to me it seems simpler and more useful to expose the named tuple directly.

Simpler: named tuples are well known. Users don’t need to learn another collection type. For key-value iteration we would write `pairs(kwargs)` which seems better to me (more explicit, and it reinforces the good pattern of using `pairs` where appropriate).

More useful: because named tuples have a richer API. For example I can get the list of values with `values(nt)`. Here I need the awkward and counter-intuitive `values(values(kwargs))`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 15, 2021, 2:09pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435/2 "2021-08-15T14:09:01Z")

</div>

The disadvantage is that a named tuple would require a different compilation for every distinct combination of kwargs passed in.

---

<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 15, 2021, 2:26pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435/3 "2021-08-15T14:26:17Z")

</div>

Couldn’t specializing on kwargs be an advantage?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [August 15, 2021, 3:10pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435/4 "2021-08-15T15:10:56Z")

</div>

How would it make a difference? Currently the function body gets an `Iterators.Pairs` of different type for each distinct combination of kwargs passed in:

```julia
julia> function f(; kwargs...)
           println(typeof(kwargs))
       end
f (generic function with 2 methods)

julia> f(a=1, b=2)
Base.Iterators.Pairs{Symbol, Int64, Tuple{Symbol, Symbol}, NamedTuple{(:a, :b), Tuple{Int64, Int64}}}

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 15, 2021, 3:17pm UTC](https://discourse.julialang.org/t/rationale-for-kwargs-as-iterators-pairs/66435/5 "2021-08-15T15:17:01Z")

</div>

we don’t specialize on kwargs either way I think
