# Metaprogramming function with keyword argument

**URL:** <https://discourse.julialang.org/t/metaprogramming-function-with-keyword-argument/12551>\
**Category:** General Usage\
**Tags:** macros, metaprogramming\
**Created:** [July 21, 2018, 4:48am UTC](https://discourse.julialang.org/t/metaprogramming-function-with-keyword-argument/12551 "2018-07-21T04:48:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lewis500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis500/32/4729_2.png) [@lewis500](https://discourse.julialang.org/u/lewis500)\
**Post date:** [July 21, 2018, 4:48am UTC](https://discourse.julialang.org/t/metaprogramming-function-with-keyword-argument/12551/1 "2018-07-21T04:48:12Z")

</div>

I am trying to make a macro that will call a function with certain keyword arguments.

For example,

```julia
add(a;b=10) = a+b

```

Now I can write

```julia
z = :(b=20)
parse("add(10,$z)")|>eval

```

and get the right answer.

```julia
But if I write
z = :(b=20)
:(add(10,$z))|>eval

```

or

```julia
:(add(10;$z)|>eval

```

I don’t get the right answer.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [July 21, 2018, 5:21am UTC](https://discourse.julialang.org/t/metaprogramming-function-with-keyword-argument/12551/2 "2018-07-21T05:21:44Z")

</div>

There is a distinction between keywords and assigments:

```julia
julia> dump(:(add(10;b=11)))                                                                                                                                             
Expr                                                                                                                                                                     
  head: Symbol call                                                                                                                                                      
  args: Array{Any}((3,))                                                                                                                                                 
    1: Symbol add                                                                                                                                                        
    2: Expr                                                                                                                                                              
      head: Symbol parameters                                                                                                                                            
      args: Array{Any}((1,))                                                                                                                                             
        1: Expr                                                                                                                                                          
          head: Symbol kw                                                                                                                                                
          args: Array{Any}((2,))                                                                                                                                         
            1: Symbol b                                                                                                                                                  
            2: Int64 11                                                                                                                                                  
          typ: Any                                                                                                                                                       
      typ: Any                                                                                                                                                           
    3: Int64 10                                                                                                                                                          
  typ: Any                                                                                                                                                               
                                                                                                                                                                         
julia> dump(:(add(10;$z)))                                                                                                                                               
Expr                                                                                                                                                                     
  head: Symbol call                                                                                                                                                      
  args: Array{Any}((3,))                                                                                                                                                 
    1: Symbol add                                                                                                                                                        
    2: Expr                                                                                                                                                              
      head: Symbol parameters                                                                                                                                            
      args: Array{Any}((1,))                                                                                                                                             
        1: Expr                                                                                                                                                          
          head: Symbol =                                                                                                                                                 
          args: Array{Any}((2,))                                                                                                                                         
            1: Symbol b                                                                                                                                                  
            2: Int64 11                                                                                                                                                  
          typ: Any                                                                                                                                                       
      typ: Any                                                                                                                                                           
    3: Int64 10                                                                                                                                                          
  typ: Any                                                                                                                                                               

```

do

```julia
julia> z = Expr(:kw, :b, 20)                                                                                                                                             
:(b=20)                                                                                                                                                                  

julia> eval(:(add(10;$z)))                                                                                                                                               
30                                                                                                                                                                       

```

---

<div class="post-metadata">

**Author:** ![lewis500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis500/32/4729_2.png) [@lewis500](https://discourse.julialang.org/u/lewis500)\
**Post date:** [July 21, 2018, 7:18pm UTC](https://discourse.julialang.org/t/metaprogramming-function-with-keyword-argument/12551/3 "2018-07-21T19:18:09Z")

</div>

Thanks Mauro! This is a good illustration of just how Expr works.

I also thought I would share: I think that the best way to do what I needed to do is with the splice … syntax. I have a function with a lot of keyword arguments, and I need to loop over different combinations of those and call the function. I hadn’t realized you can splice the keyword arguments like this

```julia
add(a=5; b=10, c=12, d=5) = a+b+c+d
params = [[(:b,4)],[(:c,18),(:d,-10)],[(:b,2),(:d,8)]]
[add(;kws...) for kws=ls]

```
