[erlang-questions] using anonymous functions when recompiling modules?

Vans S vans_163@REDACTED
Sun Apr 16 22:41:05 CEST 2017


It seems if you define an anonymous function in a module then compile and hotload the module,
once the full purge happens (after the second new version), the anonymous function ceases to exist.

-module(test).
-compile(export_all).

t() ->
    Anon = fun()->
        io:format("hi ~n", [])
    end,
    spawn(fun() ->
        (fun Loop()->
           Anon(),
           timer:sleep(2000),
           Loop()
        end)()
    end).


te() -> ok.

Now if you change te function to say tee, recompile+hotload, then change it to teee, and recompile+hotload again.

You stop seeing "hi" be printed, the process is dead, and there are no errors.

Does this mean it is simply not recommended to use anonymous functions? 
Would saving the anonymous function to the process dictionary or something keep it alive?
So many questions of why this extremely hard to debug behavior happens.



More information about the erlang-questions mailing list