# Interpolating function arguments

**URL:** <https://discourse.julialang.org/t/interpolating-function-arguments/8191>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [January 6, 2018, 12:45am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191 "2018-01-06T00:45:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 6, 2018, 12:45am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/1 "2018-01-06T00:45:33Z")

</div>

Say I have

```julia
args = :(x,y)

```

and I’d like to build

```julia
goal = :(function(x,y) 0 end)

```

The closest I’ve been able to get is

```julia
:(function($args...) 0 end)

```

but that simplifies to

```julia
:(function ((x, y)...,)
        0
    end)

```

Anyone know a way to do this?

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 6, 2018, 1:03am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/2 "2018-01-06T01:03:52Z")

</div>

Nevermind, I got it!

```julia
Main> Expr(:function, args, 0)
:(function (x, y)
        0
    end)

```

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [January 6, 2018, 1:19am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/3 "2018-01-06T01:19:01Z")

</div>

Here’s one way. `dump` is useful to figure out what needs to go in the `Expr()` call.

```julia
julia> args = :(x, y)
julia> Expr(:function, args, :(0))
:(function (x, y)
      0
  end)

```

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [January 6, 2018, 1:19am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/4 "2018-01-06T01:19:32Z")

</div>

You beat me to it! I need to learn to refresh.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 6, 2018, 1:24am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/5 "2018-01-06T01:24:21Z")

</div>

Shouldn’t this work, though?

```julia
julia> args = [:x, :y]

2-element Array{Symbol,1}:
 :x
 :y

julia> :(function($(args...)) 0 end)
       ERROR: syntax: expected "(" in function definition

```

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 6, 2018, 1:42am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/6 "2018-01-06T01:42:14Z")

</div>

> [@tshort](#):
>
> dump is useful to figure out what needs to go in the Expr() call.

Yep, good advice - this is how I figured it out too 🙂

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 6, 2018, 2:01am UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/7 "2018-01-06T02:01:41Z")

</div>

> [@cstjean](#):
>
> Shouldn’t this work, though?

Finding a lot of weird corner cases with this. Another is that `quote x end` and `:(x)` are not the same:

```julia
Main> args = (:x, :y)
(:x, :y)

Main> :($args)
(:x, :y)

Main> quote $args end
quote # none, line 1:
    (:x, :y)
end

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 6, 2018, 3:11pm UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/8 "2018-01-06T15:11:53Z")

</div>

I got it; it failed because without a comma, the parentheses were ignored:

```julia
julia> :(function ($(args...),) 0 end)
:(function (x, y) # REPL[4], line 1:
        0
    end)

```

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [January 6, 2018, 3:15pm UTC](https://discourse.julialang.org/t/interpolating-function-arguments/8191/9 "2018-01-06T15:15:52Z")

</div>

There was another one that was tricky: Turn `[:a,:b]` into `:((a,b))`. I found a solution but it’s not at all elegant. I’ll add it to [Simple metaprogramming exercises/challenges](https://discourse.julialang.org/t/simple-metaprogramming-exercises-challenges/731) .
