# How to compile Expr to function?

**URL:** <https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834>\
**Category:** New to Julia\
**Created:** [May 21, 2017, 12:34pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834 "2017-05-21T12:34:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [May 21, 2017, 12:34pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/1 "2017-05-21T12:34:21Z")

</div>

I’m trying to implement the following scenario: based on some configuration, I want to build an expression which essentially defines a function:

```julia
function build(config::String)
    :(function p() println($config) end)
    # or :(() -> println($config))
end

ex = build("test")

```

And then pass the compiled function to another method:

```julia
function test(f::Function)
    f()
end

test(eval(ex))
# prints "test"

```

The `eval` however has a side effect of introducing function `p()` (or if it has no name, then anonymous function) in the current global scope. Does anybody know how to achieve the same goal while not introducing additional definitions to the global scope, like

```julia
const f = compile(ex)
f()
# prints "test"
methods(p)
# p not defined

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 21, 2017, 12:40pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/2 "2017-05-21T12:40:48Z")

</div>

you can do:

```nohighlight
module MyEvaledFuncs

end

f = eval(MyEvaledFuncs, expr)

```

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [May 21, 2017, 12:52pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/3 "2017-05-21T12:52:12Z")

</div>

Thank you for your suggestion, but this:

```julia
function build(config::String)
    function p() println(config) end
end

const f = build("test")
f()

```

never introduces `p()` to global scope (or module scope to be precise). However I need more flexibility and construct the body of this function dynamically. Are you aware of any other methods which might be helpful here?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 21, 2017, 1:07pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/4 "2017-05-21T13:07:12Z")

</div>

Be aware that you cannot call the function immediately without going back to global scope again, don’t do this too many times and then you can use

```julia
function build(config::String)
    @eval function () println($config) end
end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 21, 2017, 3:20pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/5 "2017-05-21T15:20:37Z")

</div>

Have you checked out generated functions?

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [May 21, 2017, 9:30pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/6 "2017-05-21T21:30:44Z")

</div>

Yes, this is so far the only solution I know, with the only drawback that with each call it adds a new anonymous function to a global scope (current module). When working with a normal nested function, nothing extra goes outside, so I wondered whether the same is possible with `eval`.

Actually it is interesting whether this is a viable workaround, as suggested by @sdanisch:

```julia
function build(config)
    ex = make_body(config)
    temp = Module()
    eval(temp, :(() -> $ex))
end

```

but I suspect that this anonymous module will be hanging around till the end.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [May 21, 2017, 9:36pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/7 "2017-05-21T21:36:57Z")

</div>

Thank you for suggestion, but generated functions seem to only provide information about the type of arguments, while I build an expression at runtime based on some config object.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 21, 2017, 10:01pm UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/8 "2017-05-21T22:01:19Z")

</div>

> [@wrgr](#):
>
> ```julia
> function build(config::String)
> :(function p() println($config) end)
> # or :(() -&gt; println($config))
> end
> 
> ```

Why return an expression? Why not just return a function?

```julia
build(config::AbstractString) = () -> println(config)

```

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [May 22, 2017, 7:07am UTC](https://discourse.julialang.org/t/how-to-compile-expr-to-function/3834/9 "2017-05-22T07:07:28Z")

</div>

Because function `build` is supposed to build a function body (or entire function), based on a passed config. I just provided a minimalistic example. It could be as well something like this:

```julia
function build(config::Config)
    body = Expr(:block)
    fexpr = Expr(:function,:(make(template::Template)), body)
    push!(body.args, :(local obj::SomeObj))
    current_block = body
    for i in 1:length(config.types)
        mexpr = config.construct_expr[i]
        code = sd.codes[i]
        elseexpr = Expr(:block)
        ifexpr = Expr(:if, :(template.code == $code), :(obj = $mexpr), elseexpr)
        push!(current_block.args, ifexpr)
        current_block = elseexpr
    end
    push!(body.args, :(return obj))
    fexpr
end

```
