[erlang-questions] A tad confused about function parameters
Wed Mar 11 23:31:55 CET 2009
Here's a snippet of code that I'm using:
%% Once nodes have been started we launch the code to execute the
launch(, Pids) ->
launch([H|T], Pids) ->
S = self(), % loop(self(), start) would evaluate
self() inside loop() function
launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S, start)
%% Main benchmark loop. Receives request for particular type of test.
%% Spawns code to handle benchmark on its local node.
loop(MasterPid, Mode) ->
The line in question is:
S = self(),
-----> launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S,
I find that if I use:
launch(T, lists:append(Pids, [spawn(H, fun() ->
loop(self(), start) end)])).
instead, self() is evaluated everywhere in loop() that MasterPid occurs.
Intuitively I sort of get it, by formally I don't really understand
what's going on here...why the delayed evaluation? I would think it
evaluate before the spawn, but it's getting evaluated after the spawn
Probably another dumb question, but can someone shed some light this.
It seems like a fundamental concept to understand.
More information about the erlang-questions