# Help calling a function defined from expressions

**URL:** https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340
**Category:** Offtopic
**Tags:** question
**Created:** [January 7, 2017, 2:03pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340 "2017-01-07T14:03:56Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [January 7, 2017, 2:03pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/1 "2017-01-07T14:03:56Z")

</div>

I was using this basic pattern to define a function but it now fails when called a certain way on v0.6-dev

```julia
function test(vars)
    body = :x
    eval(Expr(:function,
              Expr(:call, gensym(), map(Symbol,vars)...),
              body))
end

fn = test(["x"]); fn(1) # works
test(["x"])(1) # fails 

```

The error is

```julia
ERROR: MethodError: no method matching ##283(::Int64)
The applicable method may be too new: running in world age 20537, while current world is 20538.
Closest candidates are:
  ##283(::Any) at :0 (method too new to be called from this world context.)

```

Is there something I should be doing differently now?

---

<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: [January 7, 2017, 2:27pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/2 "2017-01-07T14:27:39Z")

</div>

The only way would be to call `eval`. Is there some reason you need to do this, rather than defining a higher-order function in the usual way by `x -> ...`?

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [January 7, 2017, 2:41pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/3 "2017-01-07T14:41:05Z")

</div>

It comes up in `SymPy` in a “lambdify” function that walks an expression tree and creates a function from a symbolic expression, so I’m not sure I have a simple alternative, if I want to keep this functionality. I’m sure there is another workaround when I use this, but I don’t understand what makes it not work now where it had before.

---

<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: [January 7, 2017, 7:19pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/4 "2017-01-07T19:19:33Z")

</div>

> [@j\_verzani](#):
>
> I’m sure there is another workaround when I use this, but I don’t understand what makes it not work now where it had before.

This is similar to the issue discussed in [callback functions see "old world" in 0.6 · Issue #19774 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/19774), and has to do with the resolution to #265.

In Julia 0.6, when you change the definition of a function `foo`, any functions that call `foo` are recompiled the next time they are called. Technically, this involves “timestamping” each method with a “world age” that says how old it is, which is used to determine whether it needs to be recompiled. Moreover, when you are _executing_ a function, it only calls other functions from the “world” as it existed _when the function was compiled._

This affects you if you call `eval` _during_ execution of a function `foo`. Any new methods that are defined by the `eval` are in a newer world, and hence are not the functions that are called within the _same_ call to `foo`. (The _next_ call to `foo` will recompile it and see the newer world.) To see why that is, consider:

```julia
bar() = 1
function foo()
    eval(current_module(), :(bar() = "hello"))
    return bar()
end

```

What will `foo()` return? The _first_ time you call it, it will return `1`, because it will use the old version of `bar()` that was defined when `foo()` was first compiled. The _second_ time you call it, it will return `"hello"`, because it will be automatically recompiled (since it depends on `bar` and `bar` has changed). In fact, the function `bar()` will be _inlined_ when `foo()` is compiled, so there is no way for `foo()` to get the “updated” version of `bar()` without recompiling `foo()`, which can’t happen _while_ `foo()` is running — it has to wait until the next call.

Now, suppose you don’t like this behavior: you want `foo()` to return `"hello"` the _first_ time you call it, i.e. you want it to _always_ call the latest version of `bar()`. To achieve that, not only must the compiler _not inline_ the function `bar()`, it _can’t infer the return type_ of `bar()` either, because you might redefine `bar()` to return a different type (as we did above) — the function call would have to be completely _dynamic_. If the compiler did that for _every_ function you call in _any_ function, it would completely kill performance. By default, therefore, it has to call functions as they were defined when `foo()` was compiled. However, you can invoke the latest version “manually” by calling `eval`:

```julia
bar() = 1
function foo()
    eval(current_module(), :(bar() = "hello"))
    return eval(current_module(), :(bar()))
end

```

at the price of performance (no inlining or type inference). (Calling `eval` is awkward enough, in the rare cases where this is needed, that I’d prefer to have an `invokelatest` intrinsic function for this: [add invokelatest to circumvent world-age problems by stevengj · Pull Request #19784 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/19784))

A subtle design choice arises for `Function` objects that are not known at compile time, such as a _new_ function that you construct via `eval` (as in your example) or a `Function` extracted from a pointer in a callback routine (as in the issue I linked). In this case, the compiler won’t be able to inline it or (probably) do type-inference on the result, so it might as well invoke the latest-world version of the function, no? On the other hand, this makes the semantics dependent on type inference (if it can infer the specific type of function, it invokes the old-world version, but if it can only infer `Function`, then it invokes the new-world version), and it is hard to define precisely when inference will succeed.

My preference would still be to call the latest-world version of a function when its type is inferred as `Function`, since I think this is almost always what you want (especially for anonymous functions that aren’t even _defined_ in the old world), and world-age errors are awfully confusing, but reasonable people can disagree about this.

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [January 8, 2017, 3:37am UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/5 "2017-01-08T03:37:20Z")

</div>

Thanks so much for the generous and thorough explanation!

---

<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: [January 8, 2017, 11:59am UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/6 "2017-01-08T11:59:52Z")

</div>

> [@stevengj](#):
>
> My preference would still be to call the latest-world version of a function when its type is inferred as Function,

@vtjnash says that this should be improved before 0.6 is released, though I don’t know exactly what semantics he is planning: [fix REPL callbacks to use eval so newest world function is executed by KristofferC · Pull Request #19924 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/19924)

---

<div class="post-metadata">

### Author: ![klacru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klacru/32/27890_2.png) [@klacru](https://discourse.julialang.org/u/klacru)
#### Post date: [January 2, 2018, 12:44pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/7 "2018-01-02T12:44:38Z")

</div>

Sorry, that I come back to this after a year. I came across the same situation like @j_verzani repeatedly and was hoping for an improvement.

> [@stevengj](#):
>
> My preference would still be to call the latest-world version of a function when its type is inferred as Function, since I think this is almost always what you want (especially for anonymous functions that aren’t even defined in the old world), and world-age errors are awfully confusing, but reasonable people can disagree about this.

I extremely support this preference.

> [@stevengj](#):
>
> @vtjnash says that this should be improved before 0.6 is released, though I don’t know exactly what semantics he is planning: [fix REPL callbacks to use eval so newest world function is executed by KristofferC · Pull Request #19924 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/19924)

Was there a progress or a concluding discussion about this promise in the meanwhile?  
I came along the same trouble in several cases, when I created a new function by `eval` and could not call it directly, and had to use `Base.invokelatest`.  
That works fine, but it does not look clean - and it counters my intuition.

my boiled down example:

```julia
Version 0.7.0-DEV.3216 (2017-12-30 09:42 UTC)
 
julia> print1(x::String) = print2(format(x))
print1 (generic function with 1 method)

julia> print2(fmt::Function) = fmt() # does not work - use Base.invokelatest(fmt)
print2 (generic function with 1 method)

julia> format(x::String) = eval( :(() -> $x))
format (generic function with 1 method)

julia> print1("abc")
ERROR: MethodError: no method matching (::getfield(, Symbol("##5#6")))()
The applicable method may be too new: running in world age 25100, while current world is 25101.
Closest candidates are:
  #5() at REPL[7]:1 (method too new to be called from this world context.)
Stacktrace:
 [1] print2(::getfield(, Symbol("##5#6"))) at ./REPL[6]:1
 [2] print1(::String) at ./REPL[5]:1
 [3] top-level scope

julia> print2(format("abc"))
"abc"

julia> print1("abc")
ERROR: MethodError: no method matching (::getfield(, Symbol("##11#12")))()
The applicable method may be too new: running in world age 25103, while current world is 25104.
Closest candidates are:
  #11() at REPL[7]:1 (method too new to be called from this world context.)
Stacktrace:
 [1] print2(::getfield(, Symbol("##11#12"))) at ./REPL[6]:1
 [2] print1(::String) at ./REPL[5]:1
 [3] top-level scope

julia> 

```

---

<div class="post-metadata">

### Author: ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)
#### Post date: [January 27, 2018, 6:42pm UTC](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/8 "2018-01-27T18:42:28Z")

</div>

I’ve struggled a lot with this as well. But from the sounds of it this is here to stay.
