[net.lang.st80] Response to Blocks Question

scott@yale-com.UUCP (Walter Scott) (02/02/84)

	As far as I can see, in the example you present, j should get the
value zero. To show how this should happen, I'm going to have to do some
talking at the implementation (context) level, so hold on to your hat...
	First, for easy reference, here is your example again:

>    Class Awfully
>
>            silly
>                    ^ [ ^ 0 ]

and the statements that you want to execute are:

>            i <- Awfully silly .
>            j <- i value .

	Say that these statements are being executed in a context that we
shall call Context A. As the first statement is executed, the message 'silly'
is sent to Awfully, and a new context, Context B, is created. So we start
executing the bytecodes for ^ [ ^ 0 ] : A new Block Context is created, call
it Context C, and is returned as the value of the message to Context A.
	Now, back in Context A, we execute the second statement, j <- i value.
Context C is sent the 'value' message, so it becomes the active context, and
starts executing the bytecodes for ^ 0.  When a ^ appears in the bytecodes of
a block context, this indicates that a value should be returned to the Sending
Context -- the Context which invoked the Block Context's Home Context. Well,
Context C's Home Context, the context it was created in, is Context B. Context
B has already been returned from, but it still exists, because Context C still
exists and contains a reference to it; so, under Smalltalk's reference-counting
system, Context B cannot be destroyed. The Context which invoked Context B was,
of course, Context A, so this is where the value 0 will be returned to.
	The end result is that j gets the value 0 regardless of whether the
block was [ ^ 0 ] or [ 0 ] because in this case the Caller and the Sender for
the block is the same.
	The whole business of executing a block after its Home Context has
already returned leaves me wary. You can get into trouble really quick.
Suppose, for example, that we add another level to this mess. Given the method:

	class Inter
	    mediate
		^Awfully silly

and the original Awfully silly, suppose we now do:

	i <- Inter mediate.
	j <- i value.

Now here's where we get into trouble. We send the 'value' message to the block,
which then tries to return the value 0 to the context which sent the Awfully
silly message, that is, the context with the Inter mediate method. This will
cause an error, because returning to a context which has already been returned
from is illegal.

    I hope that my remarks prove helpful. If I've gotten any of this wrong,
please post additions, corrections, etc. on the net. I do not have access to an
actual Smalltalk machine, so anything I say is just the product of my own
rampant speculation.

"Keep those balloons flying high."


out of the void, into your mind...              ...from decvax!yale-comix!scott
						In-Real-Life: Walter Scott,
							      Yale University