[erlang-questions] funs and code loading

Matthias Lang matthias@REDACTED
Mon Jan 14 10:15:35 CET 2008


Hi,

I wrote this module to see how funs behave when code is reloaded:

  -module(reload).
  -export([go/0, f/0]).
  -define(VERSION, 1).
  
  go() ->
      E = fun reload:f/0, 
      F = fun() -> f() end,
      G = fun f/0,
      H = fun() -> ?VERSION end,
      spawn(fun() -> loop(E, F, G, H) end).
  
  f() ->
      ?VERSION.
  
  loop(E, F, G, H) ->
      io:fwrite("~p ~p ~p ~p\n", [E(), F(), G(), H()]),
      timer:sleep(3000),
      loop(E, F, G, H).

This is what it does on R12B-0:

   ~ >/usr/local/src/otp_src_R12B-0/bin/erl
   1> c(reload).
   {ok,reload}
   2> reload:go().
   1 1 1 1
   <0.38.0>
   3> c(reload).      % edited reload.erl to change the VERSION define to 2
   {ok,reload}
   2 2 2 1

I was expecting to see 2 1 1 1. Is there something I'm missing?

(I haven't checked whether or not (some) of the above is another facet of
http://erlang.org/pipermail/erlang-bugs/2007-June/000368.html )

Matt



More information about the erlang-questions mailing list