tags:

views:

1007

answers:

2

I keep running into this. I want to spawn processes and pass arguments to them without using the MFA form (module/function/arguments), so basically without having to export the function I want to spawn with arguments. I've gotten around this a few times using closures(fun's) and having the arguments just be bound values outside the fun(that I then reference inside the fun), but its limiting my code structure... I've looked at the docs and spawn only has the regular spawn/1 and the spawn/3 form, nothing else...

I understand that code reloading in spawned processes is not possible without the use of the MFA form but the spawned processes are not of the long running nature and finish relatively quickly so that's not an issue (I also want to contain all the code in one module-level function with sub-jobs being placed in funs inside that function).

much appreciated thanks

A: 

Short answer: you can't. Spawn (in all it's varying forms) only takes a 0-arity function. Using a closure and bringing in bound variables from the spawning function is the way to go, short of using some sort of shared data store like ETS (which is Monster Overkill).

I've never found using a closure to severely hamper my code structure, though; can you give an example of the problems you're having, and perhaps someone can tidy it up for you?

womble
+2  A: 

actually Richard pointed me in the right direction to take to avoid the issue nicelly (in a reply to the same post I put up on the Erlang GoogleGroups): http://groups.google.com/group/erlang-programming/browse_thread/thread/1d77a697ec67935a

His answer:

By "using closures", I hope you mean something like this:

Pid = spawn(fun () -> any_function(Any, Number, Of, Arguments) end)

How would that be limiting to your code structure?

 /Richard


thank you for promptly commenting you my question. Much appreciated

deepblue