# Constant propagation of kwargs possible?

**URL:** <https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810>\
**Category:** General Usage\
**Tags:** constant-propagation, kwargs\
**Created:** [August 21, 2019, 5:52pm UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810 "2019-08-21T17:52:51Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [August 21, 2019, 5:52pm UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810/1 "2019-08-21T17:52:51Z")

</div>

I suspect the answer is ‘no’, but is current Julia able to constant-propagate kwargs? In other words, is it possible to make the following infer in some way while preserving the kwarg?

Julia 1.2

```julia
julia> h(; n) = ntuple(_ -> 1, n);

julia> f() = h(n = 3);

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::Tuple{Vararg{Int64,N} where N}
...

```

(Using a positional argument instead of a kwarg works, of course, but I’d rather not give up my kwargs in my current real-world application)

```julia
julia> h(n) = ntuple(_ -> 1, n);

julia> f() = h(3);

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)

Body::Tuple{Int64,Int64,Int64}
...

```

---

<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:** [August 21, 2019, 6:36pm UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810/2 "2019-08-21T18:36:18Z")

</div>

This doesn’t directly answer your question, but I found it interesting to see that using a named tuple as a positional argument does work:

```julia
julia> h(nt) = ntuple(_ -> 1, nt.n)
h (generic function with 1 method)

julia> f() = h((n = 3,))
f (generic function with 1 method)                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                      
julia> @code_warntype f()
Body::Tuple{Int64,Int64,Int64}
1 ─ return (1, 1, 1)

```

---

<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:** [August 21, 2019, 7:54pm UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810/3 "2019-08-21T19:54:20Z")

</div>

Nice! Which probably means that constant propagation _could_ in principle be made to work also on kwargs, no? (I say this because I vaguely recall reading that kwargs work with named tuples internally, but I might be wrong)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 22, 2019, 2:41am UTC](https://discourse.julialang.org/t/constant-propagation-of-kwargs-possible/27810/4 "2019-08-22T02:41:46Z")

</div>

> [@pablosanjose](#):
>
> kwargs work with named tuples internally

Yes. See:

```julia
julia> ((; kwargs...) -> kwargs.data)(n=1) :: NamedTuple
(n = 1,)

```

You can also see that `@code_warntype f()` creates a NamedTuple.
