# Too new for this world problem when called from a function

**URL:** <https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 2, 2021, 5:43pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820 "2021-11-02T17:43:54Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 5:43pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/1 "2021-11-02T17:43:54Z")

</div>

Dear all,

As a part of a program I am developing in Julia, I would like to have boundary conditions defined as Julia functions. However, I ran into the “too new for this world problem”. It took me quite a while to come up with MWE. (It may still be a bit on the big side, but anything smaller simply wouldn’t feature the error I am getting in the real program.) Hence, apologizing in advance that it may still be too long, here it comes:

```julia
module BoundaryMod                                                               
  export Boundary                                                                
                                                                                 
  mutable struct Boundary # structure with boundary condition definition        
    value :: Function # holds functions describing boundary conditions   
    function Boundary() new((x,y,z) -> 0.0) end                                
  end                                                                            
end                                                                              
                                                                                 
module StagMod # module holding numerical method                  
  export Stag                                                                    
  export stag_allocate # allocates memory for the method                  
  export stag_compute # compute something based on boundary conditions   
  using ..BoundaryMod                                                            
                                                                                 
  mutable struct Stag                                                            
    b :: Boundary # needs boundary conditions                        
    function Stag() new(Boundary()) end                                        
  end                                                                            
                                                                                 
  function stag_allocate(s :: Stag)                                              
    s.b.value = eval(Meta.parse("function(x,y,z) return x+y end"))               
  end                                                                            
                                                                                 
  function stag_compute(s :: Stag)                                               
    println(s.b.value(1.0, 1.0, 1.0))                                            
  end                                                                            
end                                                                              
                                                                                 
using .BoundaryMod                                                               
using .StagMod                                                                   
                                                                                 
function main() # calling this creates "not from this world" problem         
  s = Stag()                                                                     
  stag_allocate(s) # allocate memory and define boundary conditions             
  stag_compute(s) # compute something with it                                  
end

```

If I load this into Julia and run `main()`, it creates the error:

```julia
ERROR: MethodError: no method matching (::Main.StagMod.var"#1#2")(::Float64, ::Float64, ::Float64)
The applicable method may be too new: running in world age 29641, while current world is 29642.
Closest candidates are:
  (::Main.StagMod.var"#1#2")(::Any, ::Any, ::Any) at none:1 (method too new to be called from this world context.)

```

Funny enough (or maybe quite logical, just beyond my understanding) if I load this file and run three functions from the `main()` from REPL, they behave just fine:

```julia
julia> s = Stag()
Stag(Boundary(Main.BoundaryMod.var"#1#2"()))

julia> stag_allocate(s)
#3 (generic function with 1 method)

julia> stag_compute(s)
2.0

```

Please let me know if you can give me a hint on how to resolve this.

_Cheers_

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 5:47pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/2 "2021-11-02T17:47:51Z")

</div>

What you see is called [world age problem](https://discourse.julialang.org/t/world-age-problem/27759) which also hit me before.

The question is: Why do you need `Meta.parse`?  
I mean, you can define your function like always and just pass it as a variable?

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 5:54pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/3 "2021-11-02T17:54:20Z")

</div>

Well, no. In reality the `stag_allocate` reads boundary conditions from a file and parses them into Julia functions. (I know this is not a good practice because it is anywhere from unsafe to downright dangerous, but I think it is easier for future users to change one line in a separate input file than to fiddle with the rest of the code.)

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 5:55pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/4 "2021-11-02T17:55:56Z")

</div>

I think, it would be better to parse those boundary conditions as variables (probably arrays?) instead of baking them into hard code.

You can still execute those functions but then you have to use `Base.invokelatest`.

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 6:06pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/5 "2021-11-02T18:06:38Z")

</div>

I was trying with `Base.invokelatets` but it didn’t lead me anywhere, really. The same error would still appear, but precisely at the call to `Base.invokelatets`. (Maybe I was doing something wrong.) Arrays would work for steady boundary conditions, but very often they are time-dependent, so one has to recompute them at the beginning of each time step.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 6:10pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/6 "2021-11-02T18:10:26Z")

</div>

```julia
julia> function wa()
           g = eval(Meta.parse("(x, y) -> x^2 + y^2"))
           Base.invokelatest(g, 1, 2)
       end
wa (generic function with 1 method)

julia> wa()
5

julia> function wa()
           g = eval(Meta.parse("(x, y) -> x^2 + y^2"))
           g(1,2)
       end
wa (generic function with 1 method)

julia> wa()
ERROR: MethodError: no method matching (::var"#17#18")(::Int64, ::Int64)
The applicable method may be too new: running in world age 29664, while current world is 29665.
Closest candidates are:
  (::var"#17#18")(::Any, ::Any) at none:1 (method too new to be called from this world context.)
Stacktrace:
 [1] wa()
   @ Main ./REPL[25]:3
 [2] top-level scope
   @ REPL[26]:1

```

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 6:11pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/7 "2021-11-02T18:11:30Z")

</div>

Yes, bet recreating new source code is even worse?

Each time, you would have an additional compilation lag.

If those numbers are passed as parameters, you compile only once.

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 6:13pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/8 "2021-11-02T18:13:45Z")

</div>

Well, that’s not the way I understand Julia works. You read boundary conditions once, compile functions describing boundary conditions also once, and for the rest of the simulation you use compiled code. Maybe I am missing something here?

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 6:15pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/9 "2021-11-02T18:15:13Z")

</div>

> [@Niceno](#):
>
> Arrays would work for steady boundary conditions, but very often they are time-dependent, so one has to recompute them at the beginning of each time step.

That confuses me? Why do arrays not work but generated functions do?

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 6:26pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/10 "2021-11-02T18:26:36Z")

</div>

Here is a typical example:  
`28.4*(0.25-(y^2))*(0.25-(z^2))*(1.0-exp(-τ))`  
Inlet velocity depends on `y` and `z` coordinates and time. Arrays holding values at boundaries would take you only so far since you have to recompute them at ever time step anyhow. Such equations I would like to parse into Julia functions at the beginning of the simulation, compile them on the first time they are invoked, and used them as compiled functions for the rest of the simulation.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 6:30pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/11 "2021-11-02T18:30:36Z")

</div>

Why doesn’t

```julia
function(y, z, τ, arr)
     arr[1]*(arr[2]-(y^2))*(arr[3]-(z^2))*(arr[4]-exp(-τ))
end

```

work?

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 6:41pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/12 "2021-11-02T18:41:01Z")

</div>

Because I am trying to make it as user input. The code is not just for me to run, and not just for one case to solve. Another example may involve sine, cosine functions, the functions change if the geometry moves and so forth.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 2, 2021, 6:43pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/13 "2021-11-02T18:43:06Z")

</div>

The best user input is a function.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 2, 2021, 6:45pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/14 "2021-11-02T18:45:45Z")

</div>

The topic was also discussed [here before](https://discourse.julialang.org/t/passing-a-string-into-function-as-a-expression/31625).

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [November 2, 2021, 8:54pm UTC](https://discourse.julialang.org/t/too-new-for-this-world-problem-when-called-from-a-function/70820/15 "2021-11-02T20:54:16Z")

</div>

Indeed. I was putting them in the wrong place. If I introduce them into the `stag_compute`, it works. It looks like this:

```julia
  function stag_compute(s :: Stag)                                               
    println(Base.invokelatest(s.b.value, 1.0, 1.0, 1.0))                                   
  end

```

But, as the Julia documentation writes: “The drawback is that `invokelatest` is somewhat slower than calling `f` directly, and the type of the result cannot be inferred by the compiler.” I could live with the “somewhat slower”, but don’t fancy the fact that " the type cannot be inferred by the compiler"
