[Grace-core] Return

James Noble kjx at ecs.vuw.ac.nz
Mon May 30 13:34:04 PDT 2011


> 1.  A return from a normal block should just terminate it and return the value (just like a call to a parameterless function with a return in it).  

yep that's right I think...

> That is essentially what should happen in the first example below, resulting in a 1 being passed along twice (with no ill effects).  I believe that semantics works fine for the second example as well as block is a closure that can be evaluated as many times as we like -- each time terminating and returning the value 1.

yes too

> 2.  Life gets complicated because we want to pass blocks as parameters to methods like "while cond do block".  In this case, performing the semantics listed above would give us the equivalent of a "continue" statement -- continue to the next iteration, as it would terminate the execution of the block, but not of the entire "while".  That is definitely NOT what we want.

right. this is what Java chose for their closure syntax, I think. I'm not sure what C# does. 

> In this case, return should perform more like an exception -- which it is, it is exceptional behavior that terminates the usual execution.  Defining "while" or "for" to catch that special exception is easily done (though nested whiles are still tricky!).

we could do it this way - the Smalltalk/Self/Newspeak answer is: return from the method lexically enclosing the current block

which leads to Michael's original question: what do you do _when the lexically enclosing method has already returned_

> The problem is how to combine 1 and 2.  If we can encode it properly, it would seem simpler to always have a return throw a special exception and then in "normal" uses of a block, have the exception caught and simply return the value.  That may mean that an in-place use of a block calls a different kind of apply (of the parameterless function encoded by the block) than when things are passed in as parameters.

hmm, is this about encoding in an implementation, or design?

> I don't have a good solution at this point, but wanted to write out my clarification of the issues.  Please correct if I've missed anything.

seems OK to me...

> It seems like other folks who have dealt with these issues (smalltalk, newspeak?) should have addressed them.  Have they?

The other option is Andrew's "modest proposal" where blocks and return statements are named:
return would return from the named enclosing object - where blocks are special kinds of objects.

that's on the stack of emails I have skimmed, but didn't managed to respond to last week.

James




More information about the Grace-core mailing list