Help with generating a function progamatically

Hi,

I’m trying to generate a function (or an expression which evaluates to function to be precise) where arguments are given by a vector of symbols, perhaps easiest explained by this example:

function fexpr()
vars = [:x,:y,:z]
quote
    function($(vars...))
        $(vars[1]) + $(vars[2]) * $(vars[3])
    end
end
end

The above fails (doesn’t compile) with the error message ERROR: syntax: expected "(" in function definition.

In case it is not clear, I would like the above to result in the same as this:

function fexpr()
vars = [:x,:y,:z]
quote
    function(x,y,z)
        $(vars[1]) + $(vars[2]) * $(vars[3])
    end
end
end
julia> vars = [:x, :y, :z]
3-element Array{Symbol,1}:
 :x
 :y
 :z

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

julia> :(function($(vars...),) end)
:(function (x, y, z)
      #= REPL[3]:1 =#
  end)
2 Likes

Thanks alot!

Just for my own education, what is the rationale behind the trailing comma? Is it just for disambiguation?