Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Those statements of "discarding the stack" confused me also for a while and I think I get it now and this is my understanding now. In case of "receive" there is a dedicated thread blocking on the message (using object.wait() on a monitor) and this means that the complete thread stack is available and ready to continue from the point of "waiting" on receiving a message. For example if you had the following code </p> <pre><code> def a = 10; while (! done) { receive { case msg =&gt; println("MESSAGE RECEIVED: " + msg) } println("after receive and printing a " + a) } </code></pre> <p>the thread would wait in the receive call until the message is received and then would continue on and print the "after receive and printing a 10" message and with the value of "10" which is in the stack frame before the thread blocked. </p> <p>In case of react there is no such dedicated thread, the whole method body of the react method is captured as a closure and is executed by some arbitrary thread on the corresponding actor receiving a message. This means only those statements that can be captured as a closure alone will be executed and that's where the return type of "Nothing" comes to play. Consider the following code</p> <pre><code> def a = 10; while (! done) { react { case msg =&gt; println("MESSAGE RECEIVED: " + msg) } println("after react and printing a " + a) } </code></pre> <p>If react had a return type of void, it would mean that it is legal to have statements after the "react" call ( in the example the println statement that prints the message "after react and printing a 10"), but in reality that would never get executed as only the body of the "react" method is captured and sequenced for execution later (on the arrival of a message). Since the contract of react has the return type of "Nothing" there cannot be any statements following react, and there for there is no reason to maintain the stack. In the example above variable "a" would not have to be maintained as the statements after the react calls are not executed at all. Note that all the needed variables by the body of react is already be captured as a closure, so it can execute just fine. </p> <p>The java actor framework <a href="http://www.malhar.net/sriram/kilim/" rel="noreferrer">Kilim</a> actually does the stack maintenance by saving the stack which gets unrolled on the react getting a message. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload