# Evaluating a SyntaxTree.genfun within a function

**URL:** <https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744>\
**Category:** Specific Domains\
**Tags:** package, metaprogramming\
**Created:** [September 17, 2020, 3:16am UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744 "2020-09-17T03:16:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jhall7529](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@Jhall7529](https://discourse.julialang.org/u/Jhall7529)\
**Post date:** [September 17, 2020, 3:16am UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/1 "2020-09-17T03:16:25Z")

</div>

Im trying to evaluate a string as a function within a function. I have made good progress:

```julia
using SyntaxTree
function Evaluate(some_function::String ; variables = [:x])
    g = genfun(Meta.parse(some_function), variables)
    return g
end

```

This allows me to write Evaluate(“some function”)(x)  
which is good, but if I want to evaluate this inside another function, for example:

```julia
function foo(some_function::String ; variables = [:x])
     x = 1
     return Evaluate(some_function)(x)
end

```

I get an Error

```julia
MethodError: no method matching (::getfield(SyntaxTree, Symbol("##137#138")))(::Float64)
The applicable method may be too new: running in world age 26255, while current world is 26256.
Closest candidates are:
  #137(::Any) at /Users/joe/.julia/packages/SyntaxTree/Adq4Y/src/SyntaxTree.jl:144 (method too new to be called from this world context.)

Stacktrace:
 [1] h(::Float64) at ./In[113]:1
 [2] WaveFunctionNumerov(::String, ::Float64, ::Int64, ::Float64, ::Float64, ::Float64) at ./In[113]:24
 [3] top-level scope at In[114]:52

```

Why is this a problem in the first place and how can I get around it?

the evaluation really needs to be within a function

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 18, 2020, 1:10pm UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/2 "2020-09-18T13:10:37Z")

</div>

> [@Jhall7529](#):
>
> Im trying to evaluate a string as a function within a function.

First thing first: _why?_  
I ask because I see people try to do this a lot, and it has _never_ been the right answer.

That being said, it looks like `SyntaxTree` offers `genlatest` which should work, since it calls `Base.invokelatest`, getting around the world age issue. Again, this is probably the wrong approach to the problem, with plenty of negative performance and runtime consequences, so I urge you to reconsider.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 18, 2020, 1:36pm UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/3 "2020-09-18T13:36:20Z")

</div>

Hi, i am the creator of `SyntaxTree` and I recommend using Julia expressions for metaptogramming if possible instead of parsing strings, as @tomerarnon mentioned it is not so good to use strings for this in general.

Indeed, the purpose of `genlatest` is to get around the world age issues.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 18, 2020, 2:21pm UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/4 "2020-09-18T14:21:20Z")

</div>

The problem is that `genfun` most likely uses `eval` and `eval` always runs at runtime. Therefore, when the compiler looks at `foo`, `eval` has not been run yet, so it doesn’t have any way of knowing what to do with the call `Evaluate(some_function)(x)`. This is what it means for `g` to only be defined in a newer world age. You can use `Base.invokelatest`, to work around this, but that has quite a big performance penalty. An alternative might be to use [RuntimeGeneratedFunctions.jl](https://github.com/SciML/RuntimeGeneratedFunctions.jl), which can build functions from expressions at runtime without the use of `eval`, but is a bit hacky.

---

<div class="post-metadata">

**Author:** ![Jhall7529](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@Jhall7529](https://discourse.julialang.org/u/Jhall7529)\
**Post date:** [September 20, 2020, 2:40am UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/5 "2020-09-20T02:40:32Z")

</div>

The idea is that when running the julia script, the script will ask for an input, (in the form of a String or Expr) and then run the program from there making the program more dynamic and diverse.

I will try the genlatest, although I’d be more than happy to hear a more effective solution if you have one?

---

<div class="post-metadata">

**Author:** ![Jhall7529](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@Jhall7529](https://discourse.julialang.org/u/Jhall7529)\
**Post date:** [September 20, 2020, 2:42am UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/6 "2020-09-20T02:42:31Z")

</div>

Why would this make a difference? Surely parsing a String into an Expr is effectively the same thing as just taking an Expr as input?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 23, 2020, 9:30pm UTC](https://discourse.julialang.org/t/evaluating-a-syntaxtree-genfun-within-a-function/46744/7 "2020-09-23T21:30:08Z")

</div>

True, in this case using a string or an expr would be the same (and suffer from the same limitations).  
If you intend to create a function from a user inputted string, consider separating the `Evaluate` function into two separate operations in your script. First create the `genfun` function (which will no longer need to be `genlatest`, as a result of this split) and then pass the created function on to another function to do the work. This will ensure type stability and will therefore be more performant.

```julia
g = Evaluate(user_input) # type unstable, but who cares?
foo(g) # foo is specialized to typeof(g) now!

```
