[newbie] spawn-ed functions always exported?

Richard Carlsson richardc@REDACTED
Thu Jan 27 09:07:34 CET 2005


(Seems I forgot to cc the list on my first attempt to answer.
Darned gooey reply-buttons.)

Carsten Schultz wrote:
> Instead of
> 
>     spawn(?MODULE, f, [A1, A2, A3])
> 
> you can use
> 
>     spawn(fun ()->f(A1, A2, A3) end)
> 
> Note however that these two will have slightly differing behaviour in
> the presence of hot code update.  Afaik the first one will spawn the
> function of the newest version of the module, while the second one
> will spawn the function of the version of the module executing the
> spawn function.

True. The following will behave like the first version:

     spawn(fun () -> ?MODULE:f(A1, A2, A3) end)

This might also be a good time to point out that the fun-object
that is created and passed to spawn/1 will become garbage as soon
as the process is spawned, since the body of the fun is a tail
call. Replacing the code of the module that the fun belongs to
will *not* affect the created process. (I've sometimes heard
people worry about code change when using spawn/1.)

	/Richard



More information about the erlang-questions mailing list