# Quote with a list of kwargs

**URL:** <https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [September 18, 2019, 9:29am UTC](https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879 "2019-09-18T09:29:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [September 18, 2019, 9:29am UTC](https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879/1 "2019-09-18T09:29:46Z")

</div>

The following code is a MWE of what I’m trying to do; I’d like to write the inside of the `foo` function as a `quote` if possible (in order to make the code in my use case more readable and maintainable).

```julia
function foo(fname, pnames, pvals)
    Expr(:function, Expr(:call, fname, Expr(:parameters, (Expr(:kw, p, v) for (p, v) in zip(pnames, pvals))...)),
        Expr(:block,
            :(return nothing)
            )
        )
end

```

the output of the function is what I want, i.e. if `eval(foo(:bar, (:a1, :a2), (0, 1)))` we get a `bar` function with two kwargs `a1=0` and `a2=1`

I’d like to write it more like the following code but I don’t know what I should write in `...`

```julia
function foo2(fname, pnames, pvals)
    quote
        function $fname(...)
            return nothing
        end
    end
end

```

Thanks

---

<div class="post-metadata">

**Author:** ![mateuszbaran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mateuszbaran/32/221842_2.png) [@mateuszbaran](https://discourse.julialang.org/u/mateuszbaran)\
**Post date:** [September 18, 2019, 9:48am UTC](https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879/2 "2019-09-18T09:48:08Z")

</div>

This, at least, is an improvement over the first version:

```julia
function foo2(fname, pnames, pvals)
    params = [Expr(:kw, p, v) for (p, v) in zip(pnames, pvals)]
    quote
        function $fname($(params...))
            return nothing
        end
    end
end

```

I don’t know how make `params` nicer though.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [September 18, 2019, 10:04am UTC](https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879/3 "2019-09-18T10:04:18Z")

</div>

Thanks Mateusz, I had played with things like that but putting the comprehension inside the quote and that wasn’t working, this is simple and does the job 🙂 🎖

Edit, for people who may have been looking for something similar this works: (Mateusz version doesn’t create keyword args)

```julia

function foo2(fname, pnames, pvals)
    kw = (Expr(:kw, p, v) for (p, v) in zip(pnames, pvals))
    quote
        function $fname(;$(kw...))
            return nothing
        end
    end
end

```

---

<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:** [September 18, 2019, 10:19am UTC](https://discourse.julialang.org/t/quote-with-a-list-of-kwargs/28879/4 "2019-09-18T10:19:26Z")

</div>

I think the problem is that parsing of `a = b` is context dependent, either `Expr(:=, ...)` in normal cases or `Expr(:kw, ...)` after `;` in function definitions and named tuples. And AFAIK there is no easy way to create a list of `Expr(:kw)` to splat after the `;` except with the literal syntax `:((; a = b, c = d))` but here we want to do it programmatically.
