# Defining function from string with parse and eval failure in v0.6 master

**URL:** <https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249>\
**Category:** General Usage\
**Created:** [April 17, 2017, 4:04pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249 "2017-04-17T16:04:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JuhaHeiskala](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@JuhaHeiskala](https://discourse.julialang.org/u/JuhaHeiskala)\
**Post date:** [April 17, 2017, 4:04pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249/1 "2017-04-17T16:04:09Z")

</div>

Hello,

The following seems like a bug, but maybe there is something I am missing about eval and parse that is causing the failure.  
I define anonymous function and pass it as a parameter to another function. The code works when the anonymous function is defined directly as lambda, but fails when it is defined with parse and eval.

Contest of my file “test\_func.jl”

```julia
function foo1()
    return (x,y)->(x==y)
end

function foo2()
   return eval(parse("(x,y) -> (x==y)"))
end

function foo3(f, x,y)    
    f(x,y)
end

# this works with function defined directly as lambda in foo1
f1=foo1()
foo3(f1,1,2)
foo3(foo1(),1,2)

# this works with foo2 returning a function defined with parse and eval
f2=foo2()
foo3(f2,1,2)

# this fails with the parse and eval defined function passed directly as parameter to another function
foo3(foo2(),1,2)
```

This is with master from build today.

```julia
               _
   _ _ _(_)_ | A fresh approach to technical computing
  (_) | (_) (_) | Documentation: http://docs.julialang.org
   _ _ _| |_ __ _ | Type "?help" for help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 0.6.0-pre.beta.180 (2017-04-16 23:46 UTC)
 _/ |\ __'_|_|_|\__'_| | Commit 934e0e3 (0 days old master)
|__/ | x86_64-linux-gnu

julia> include("test_func.jl")
ERROR: LoadError: MethodError: no method matching (::##5#6)(::Int64, ::Int64)
The applicable method may be too new: running in world age 21669, while current world is 21670.
Closest candidates are:
  #5(::Any, ::Any) at none:1 (method too new to be called from this world context.)
```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 17, 2017, 5:23pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249/2 "2017-04-17T17:23:30Z")

</div>

You’re experiencing [https://docs.julialang.org/en/latest/manual/methods.html#Redefining-Methods-1](https://docs.julialang.org/en/latest/manual/methods.html#Redefining-Methods-1) which is a breaking change in Julia v0.6. This change was made to avoid a long-standing issue in Julia in which newly-defined functions sometimes wouldn’t take effect: [https://github.com/julialang/julia/issues/265](https://github.com/julialang/julia/issues/265) Julia v0.6 is stricter (which is annoying in your particular case) but also more correct (which is better in all cases).

It might be worth considering whether you _really_ need to be defining a function with `eval()`. There may be an alternative that doesn’t require jumping through these hoops.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 17, 2017, 5:26pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249/3 "2017-04-17T17:26:53Z")

</div>

On the other hand, I admit I was very surprised to see that

```julia
f2=foo2()
foo3(f2,1,2)

```

works, but

```julia
foo3(foo2(),1,2)

```

does not, even when the code is put inside a script or module.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 17, 2017, 5:31pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249/4 "2017-04-17T17:31:05Z")

</div>

Ah, I see, the distinction between those two shows up only at global scope. Putting the calls to `foo2()` and `foo3` inside a function results in

```julia
f2=foo2()
foo3(f2,1,2)

```

also failling, as expected.

---

<div class="post-metadata">

**Author:** ![JuhaHeiskala](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@JuhaHeiskala](https://discourse.julialang.org/u/JuhaHeiskala)\
**Post date:** [April 18, 2017, 6:31pm UTC](https://discourse.julialang.org/t/defining-function-from-string-with-parse-and-eval-failure-in-v0-6-master/3249/5 "2017-04-18T18:31:23Z")

</div>

Thanks, got it to work with the double eval after reading the manual.

This came up with experimenting with runtime defined functions from strings.
