Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Do funs behave during a code change?

code funs
0
10 Posted

Do funs behave during a code change?

0

Yes and no. A fun is a reference to code; it is not the code itself. Thus a fun can only be evaluated if the code it refers to is actually loaded on the evaluating node. In some cases, we can be sure that execution will never return to a fun. In these cases the old code can be purged without problems. The following code causes no surprises: Parent = self(), F = fun() -> loop(Parent) end, spawn(F). On the other hand, a bound fun will not be replaced. In the following example, the old verson of F is executed even after a code change: -module(cc). -export([go/0, loop/1]). go() -> F = fun() -> 5 end, spawn(fun() -> loop(F) end). loop(F) -> timer:sleep(1000), F(), cc:loop(F). This type of problem can be solved in the code_change/2 function in the standard behaviours.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123