[Grace-core] Return

Andrew P. Black black at cs.pdx.edu
Tue May 31 01:05:24 PDT 2011


On 30 May 2011, at 11:46 , Kim Bruce wrote:

> 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).  

I disagree.  A return in a block should terminate the method in which the block was written.

I have suggested named exits to make it possible to get other effects, and also to make it possible to explain what return does.

So:

> method usefulReturn {
>   return 7
> }


would translate to 

method usefulReturn methodBody: {
	exit methodBody with 7
}
> .
> 
> 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, it's not what we want.  What we want is for the innermost block with the return in

method contains(x) {
	while { a := a.rest } do { if x = a.head then {return true}} 
	return false
}

to terminate the method contains — not the if()then() and not the while()do().   Again, with labelled blocks we can make this explicit:

method contains(x) methodBody:{
	while { a := a.rest } do { if x = a.head then {exit methodBody with true}} 
	exit methodBody with false
}


> 
> 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.

I disagree.  I don't think that returning form a method is an exception and I don't think that it can be caught.  But maybe I'm missing the rationale for this.

	Andrew




More information about the Grace-core mailing list