[erlang-questions] what is the correct syntax to spawm a MFA with no arguments?

Richard Carlsson richardc@REDACTED
Thu May 1 12:04:49 CEST 2008


Darren New wrote:
> I know very little about Erlang overall, but isn't the point of 
> exporting this to make it available for code upgrades? I.e., without 
> exporting the entry, it would be very difficult to have spawn(M,F,A) 
> spawn code that had changed recently, yes?

No, because if you are using spawn(fun loop/0), or if you want to pass
some arguments, use spawn (fun () -> loop(X, Y, Z) end), then obviously
both the spawning code and the code for the fun is in the _same module_,
so if the spawner is running the new code, the new process will also
be running the new code. Code upgrade is only done per whole module.

Also note that since the body of the fun is a tail call to the loop
function, the fun-object is just a temporary object which is discarded
once the new process starts executing in loop(). So, spawning from
funs in this way is totally safe with respect to code upgrade.

The warning about funs and code upgrade only has to do with stuff like
storing a fun in a data structure somewhere, and then later digging it
out and running it. It is by that time possible that the module where
the fun was defined has been reloaded, but you'll still be running the
old code that the fun belongs to. (You might often actually want this.)
When you do a spawn from a literal fun for code in the same module, this
cannot happen.

Don't export internal functions. Always use spawn(fun ...), unless
you feel that you _must_ be able to have a varying parameter list.
(And in that case I'll come over and slap you silly, the lot of you.)

     /Richard



More information about the erlang-questions mailing list