# 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:** 1\
**Showing post:** 4

<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.

---

_[View the full topic](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340)._
