# How to avoid evaluation into the global space when the expression is not know in advance

**URL:** <https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342>\
**Category:** General Usage\
**Tags:** strings, eval\
**Created:** [August 30, 2021, 11:41am UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342 "2021-08-30T11:41:10Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 1:58pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/21 "2021-08-30T13:58:24Z")

</div>

Do you mean that the when iterating the function list, every function, not just the current one in use, will be compiled ?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 30, 2021, 2:01pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/22 "2021-08-30T14:01:12Z")

</div>

Well if you parse & create functions from a file, they will be new anonymous functions. Every julia function (no matter whether it’s anonymous or named) is compiled on first call. If you create new functions, they will be compiled. If you create two anonymous functions with the same contents, they will be compiled seperately since they’re distinct functions:

```julia
julia> a = (x,y) -> x*y                                                  
#1 (generic function with 1 method)                                      
                                                                         
julia> b = (x,y) -> x*y                                                  
#3 (generic function with 1 method)                                      
                                                                         
julia> b === a                                                           
false                                                                    
                                                                         
julia> @time a(1,2)                                                      
  0.003943 seconds (749 allocations: 62.094 KiB, 99.42% compilation time)
2                                                                        
                                                                         
julia> @time b(1,2)                                                      
  0.007552 seconds (747 allocations: 46.547 KiB, 98.99% compilation time)
2                                                                        

```

> [@JianghuiDu](#):
>
> Do you mean that the when iterating the function list, every function, not just the current one in use, will be compiled ?

No, I mean that every function, on first use, will be compiled. If you only use a function once and then never again, that’s a lot of compilation time. Moreover, if you then create a different anonymous function with the same inner workings (=syntax) parsed from two different files (or even the same non-julia file, just used twice), they are distinct and will be compiled distinctly.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 30, 2021, 2:03pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/23 "2021-08-30T14:03:19Z")

</div>

But really, if you are worried about performance, that is a completely different story. It seems to me that you should not worry about that for this specific application. (but then of course we would need to know much more about what you are trying to do to actually suggest something there).

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 2:14pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/24 "2021-08-30T14:14:03Z")

</div>

I’m more concerned about avoiding conflict in the namespace. Performance is not a big issue.

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 2:14pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/25 "2021-08-30T14:14:20Z")

</div>

Thanks for the explanation!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 30, 2021, 2:21pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/26 "2021-08-30T14:21:46Z")

</div>

If the anonymous function syntax is too strange (I’m thinking about high-school students, perhaps), you could simply as them to write `f(x,y) = x*y` in each field, and do:

```julia
julia> str = "f(x,y) = x*y"
"f(x,y) = x*y"

julia> functions = [eval(Meta.parse(replace(replace(str, "f" => ""),"=" => "->")))]
#3 (generic function with 1 method)

julia> functions[1](2,3)
6

```

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 2:24pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/27 "2021-08-30T14:24:39Z")

</div>

I think I just need “x\*y”. I can create the string expression by add “(x,y)-\>” in front. Thanks for you ideas!

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [August 30, 2021, 2:41pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/28 "2021-08-30T14:41:23Z")

</div>

It’s probably obvious for most people here but I think it’s necessary to add to be very **VERY** careful when using `eval` on arbitrary code.

with `eval`, your friend can do basically anything on your computer. Be sure to 100% trust your friend and/or check manually before executing the code

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 2:44pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/29 "2021-08-30T14:44:31Z")

</div>

What dangerous things can `eval` do? If it isn’t a correct julia expression then it should just abort, right?

---

<div class="post-metadata">

**Author:** ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)\
**Post date:** [August 30, 2021, 2:46pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/30 "2021-08-30T14:46:54Z")

</div>

everything Julia _can_ do, which means read all your personal files, download or upload anything…

for example (you can imagine replacing `println` with `rm` or some kind of upload)

```julia
(x,y) -> (println(readdir(".")); return x+y)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 30, 2021, 2:50pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/31 "2021-08-30T14:50:22Z")

</div>

That should be a primary concern if you are willing to provide that interface as a web service, for example. If the user will be running from his/her own computer, it is just as writing any code.

---

<div class="post-metadata">

**Author:** ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)\
**Post date:** [August 30, 2021, 3:05pm UTC](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342/32 "2021-08-30T15:05:00Z")

</div>

Of course… Thanks for the reminder.

[Previous page](https://discourse.julialang.org/t/how-to-avoid-evaluation-into-the-global-space-when-the-expression-is-not-know-in-advance/67342.md?page=1)
