using anonymous functions when recompiling modules?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

using anonymous functions when recompiling modules?

Vans S
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.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: using anonymous functions when recompiling modules?

Éric Pailleau
Hi,
Changing code in a running process needs to call a function prefixed by the module name.
test:t() in your case.
Your process is running always the first version until second version becomes old version and third version the new version.
When first version vanish,  processes using this one die.
Nothing to do with anonymous functions,  you will have same behavior with functions without reentrant module name prefix.


Le 16 avr. 2017 10:41 PM, Vans S <[hidden email]> a écrit :

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.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...