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

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

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> 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                                                           
julia> @time a(1,2)                                                      
  0.003943 seconds (749 allocations: 62.094 KiB, 99.42% compilation time)
julia> @time b(1,2)                                                      
  0.007552 seconds (747 allocations: 46.547 KiB, 98.99% compilation time)

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.


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

1 Like

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

1 Like

Thanks for the explanation!

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

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

1 Like

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


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

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)

(x,y) -> (println(readdir(".")); return x+y)
1 Like

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.

Of course… Thanks for the reminder.