The easiest and usually best way is to use quotes and interpolate into
them:

fname = :f1
:($fname(x::Int) = x+31)

But that does not always work.  Then you have to resort to manipulating
expressions which is a bit complicated.  xdump is your friend.  Also
note that you can construct them like so:

Expr(:call, [:+, :x, 31]...)

Have a look at https://github.com/burrowsa/MetaTools.jl and
https://github.com/one-more-minute/MacroTools.jl

On Wed, 2015-09-30 at 11:20, Andreas Lobinger <[email protected]> wrote:
> Hello colleagues,
>
> one of the USPs of julia is (should be) metaprogramming i.e. creating programs
> by running other programs.
> I'm playing around with Expr to create functions, but this look more
> complicated than the actual job...
>
> # three versions
>
> function f1(x::Int64)
>     x+31
> end
>
> function fe2a()
>     e0 = Expr(:function,
>         Expr(:call,:f2a,
>             Expr(symbol("::"),:x,:Int64)),
>         Expr(:call,:+, :x, 31))
>     e0
> end
>
> function fe2b()
>     e1 = Expr(:call)
>     e1.args = [:+, :x, 31]
>     ef = Expr(symbol("::"))
>     ef.args = [:x, :Int64]
>     e2 = Expr(:call)
>     e2.args = [:f2b,ef]
>     e3 = Expr(:function)
>     e3.args = [e2,e1]
>     e3
> end
>
>
> afaics Expr needs constant arguments, but the expression created allows
> changing fields (like args). Could Expr be extended, so that it also accepts
> Arrays as arguments?

Reply via email to