How can I avoid creating closures? I noticed that due to the Julia syntax/REPL I inadvertently create a lot of closures. Consider my typical workflow where code evolves from a unstructured script towards a more function based interface: I have a bunch of lines that do what they are supposed to, and then I want to encapsulate the task and create a clean interface. When I place the lines in a function I repeatedly forget some variables that either should be arguments to the function or computed from the arguments. Below is a minimal example that runs and returns 5:
function fun(x)
a + x
end
a = 2
fun(3)
I forgot to make the a an argument. This runs fine now, but it will break in an other context. I thought the compiler should warn me about this? Isnβt this a step backwards?
Can I tell the function not to capture or the variable a not to be captured?
All you need to to is put your functions inside a module, which will prevent them from accessing globals from the REPL. Of course, any global variables inside the module will still be accessible, but those should be much easier to avoid.