# Eval() fails inside a function when it works stand alone

**URL:** <https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846>\
**Category:** General Usage\
**Created:** [February 6, 2024, 11:50pm UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846 "2024-02-06T23:50:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [February 6, 2024, 11:50pm UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/1 "2024-02-06T23:50:51Z")

</div>

I have this code, which works just fine outside of the function but when I enclose it inside the function it fails at the eval() command with the following hard to decipher message. I have looked at other similar problems but I do not understand how to fix this one. Any help will be appreciated. Cheers 🙂

MethodError: no method matching (::var"#87#88")(::Float64)  
The applicable method may be too new: running in world age 33916, while current world is 33917.

Closest candidates are:  
(::var"#87#88")(::Any) (method too new to be called from this world context.)  
@ Main C:\Users\PWC.julia\packages\SymbolicUtils\ssQsQ\src\code.jl:373

```
function testing(time)
    @variables t
    
    D = Differential(t)
    
    c = 0.06 * pi # Used to make the mathematical code clearer only.
    C = 0.3 # Capacitance in Farad.
    L = 1.7 # Inducatance in Henry.
    R = 1.4 # Resistance in Ohms.
    
    # This the function as given in the assignment question.
    v_equ = (exp(-c*t) * sin(2*t - pi))
    
    # This calculates the 1st derivative
    dv_equ = D(v_equ)
    
    # This calcultes the 2nd derivative
    dv2_equ = D(dv_equ)
    
    # This is the full function for di/dt as a function of t only
    di_equ = expand_derivatives(C * dv2_equ + 1/R * dv_equ + v_equ/L)
    
    f_i=build_function(integrate(di_equ, symbolic=true, detailed=false),t)
    f_i=eval(f_i)
      
    current=f_i.(time)-f_i.(zeros(size(time)))
   return(time,current) 
end

time,current=testing(time)

plot(time,current,label="Integrated")
plot!(Runga_time,Runga_current, label="Runga")
plot!(Euler_time,Euler_current,label="Euler")

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [February 7, 2024, 1:47am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/2 "2024-02-07T01:47:19Z")

</div>

You’ll want to try `invokelatest`, documented [here](https://docs.julialang.org/en/v1/manual/methods/#Redefining-Methods). That’s the usual fix for world-age problems.

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [February 7, 2024, 2:32am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/3 "2024-02-07T02:32:47Z")

</div>

Thank you, I did discover that command, however, I have no idea how or where to use it as the example in the reference you gave is totally different to what I am doing.

I will leave the code in the main body for the time being, it is just untidy and I don’t like doing so.

I was hoping Julia was ‘better’ than other languages and would not have all these weird issues going on everywhere ☹

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 7, 2024, 3:16am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/4 "2024-02-07T03:16:10Z")

</div>

No, the answer is almost never “use `eval` and `invokelatest`”. Instead the answer is to avoid using `eval`.

In a situation like this, I’d recommend RuntimeGeneratedFunctions.jl instead.

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [February 7, 2024, 3:59am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/5 "2024-02-07T03:59:17Z")

</div>

Thank you,

Is there any chance you might indicate how to use this in my code please?

I have included the module RuntimeGeneratedFunctions but like most other documentation, the simple example is far from simple and really gives me exactly no clue what is required to incorporate this into my code.

I am getting an error but I have no understanding of what it means.

Cheers, Paul

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/63dec8a7c299199865458776bfa67d3aa7aa3cd8.png)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 7, 2024, 4:04am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/6 "2024-02-07T04:04:55Z")

</div>

That error is telling you that there’s not a macro named `@RuntimeGeneratedFunctions` defined — it’s not defined because it’s misspelled. It should be singular: `@RuntimeGeneratedFunction`.

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [February 7, 2024, 4:15am UTC](https://discourse.julialang.org/t/eval-fails-inside-a-function-when-it-works-stand-alone/109846/7 "2024-02-07T04:15:35Z")

</div>

Thank you so much, and i am sorry for the silly mistake.

It is working as it should now, with three different method of solving my differential equation producing almost identical results. 🙂

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/7/d75aeec14435a59edd14cb29b05b6305a1c3b0b0.png)
