# Passing Keyword Arguments to Expr Object with :call

**URL:** <https://discourse.julialang.org/t/passing-keyword-arguments-to-expr-object-with-call/17315>\
**Category:** General Usage\
**Created:** [November 8, 2018, 6:28pm UTC](https://discourse.julialang.org/t/passing-keyword-arguments-to-expr-object-with-call/17315 "2018-11-08T18:28:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elperkerson62](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elperkerson62/32/597_2.png) [@elperkerson62](https://discourse.julialang.org/u/elperkerson62)\
**Post date:** [November 8, 2018, 6:28pm UTC](https://discourse.julialang.org/t/passing-keyword-arguments-to-expr-object-with-call/17315/1 "2018-11-08T18:28:11Z")

</div>

I have a function f that has many different versions (e.g. f\_A, f\_B, f\_C, …), “subversions” (e.g. f\_Aa, f\_Ab, …, f\_Ba, f\_Bb, …), “subsubversions” (e.g. f\_Aaa, …), and so forth. Each of these takes a large number of parameters, most of which they have in common but some they don’t. I want to test the different versions of this function in a loop because there are so many of them. Perhaps something like:

```julia
f_A(;x, y, unused...) = x + y
f_B(;x, z, unused...) = x + z
function f(version; kwargs...)
    return eval(Expr(:call, Symbol("f_", version), kwargs...))
end

for ver = ["A", "B"]
    f(ver, x=1, y=2, z=3) # Throws error
end

```

It seems that the keyword arguments are passed to the function f\_A or f\_B as Pairs instead of kw arguments. For example, using range as an example of a built-in function with keyword arguments,

```julia
expr1 = Meta.parse("range(1, stop=2, length=5)")
Meta.dump(expr1)

```

Returns the following:

Expr  
head: Symbol call  
args: Array{Any}((4,))  
1: Symbol range  
2: Int64 1  
3: Expr  
head: **Symbol kw**  
args: Array{Any}((2,))  
1: Symbol stop  
2: Int64 2  
4: Expr  
head: **Symbol kw**  
args: Array{Any}((2,))  
1: Symbol length  
2: Int64 5

Whereas my expression looks like this:

```julia
function get_expr(version; kwargs...)
    return Expr(:call, Expr(Symbol("f_", version), kwargs...))
end
expr2 = get_expr("A", x=1, y=2, z=3)
Meta.dump(expr2)

```

Returns:

Expr  
head: Symbol call  
args: Array{Any}((1,))  
1: Expr  
head: Symbol f\_A  
args: Array{Any}((3,))  
1: **Pair{Symbol,Int64}**  
first: Symbol x  
second: Int64 1  
2: **Pair{Symbol,Int64}**  
first: Symbol y  
second: Int64 2  
3: **Pair{Symbol,Int64}**  
first: Symbol z  
second: Int64 3

So how can I get the keyword arguments to be passed to f\_A or f\_B as Symbol kw instead of Pair{Symbol,Int64}?

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [November 8, 2018, 6:56pm UTC](https://discourse.julialang.org/t/passing-keyword-arguments-to-expr-object-with-call/17315/2 "2018-11-08T18:56:34Z")

</div>

You’re gonna need something like

map(pair → Expr(:kw, pair.first, pair.second), kwargs)

But before you do this, I have a couple of suggestions:

1. Implement this as a macro instead
2. Refactor your functions. In general, using postfixes to create a family of related functions in programming suggests that you have a problem which could better be solved by multiple dispatch (and the devs feel so strongly about this that they’ve nixed underscores from Base)

---

<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:** [November 8, 2018, 10:20pm UTC](https://discourse.julialang.org/t/passing-keyword-arguments-to-expr-object-with-call/17315/3 "2018-11-08T22:20:45Z")

</div>

> [@bramtayl](#):
>
> Implement this as a macro instead

If it is just used there in the testing, then a macro is not needed. Just a loop with `eval`s is the ticket.
