[erlang-questions] A tad confused about function parameters

Richard Andrews <>
Thu Mar 12 00:43:43 CET 2009




> The line in question is:
>             S = self(),
> ----->     launch(T, lists:append(Pids, [spawn(H, fun() -> loop(S,  
> start) end)])).
> 
> 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  
> would be
> evaluate before the spawn, but it's getting evaluated after the spawn  
> on node
> 'H'.

As I understand it, function calls in the body of a fun are executed when the fun is
executed but bindings are copied from the local context when the fun is
constructed.

In the first case S is a binding made before launch() is called. S is copied into the fun object and will not change. In the second case the instructions in the fun say it must call self() when the fun is executed.

--
  Rich


      Stay connected to the people that matter most with a smarter inbox. Take a look http://au.docs.yahoo.com/mail/smarterinbox



More information about the erlang-questions mailing list