jastrachan | 26 Jan 16:16 2005
Picon

Re: [groovy-dev] Diff of Java and Groovy grammars

On 26 Jan 2005, at 14:51, Guillaume Laforge wrote:
>> String foo() {
>>     while (something) {
>>          coll.each { |p| if (p == 1000) { return } else println p }
>>     }
>> }
>>
>> The 'return' in the above should return from the function foo().
>>
>> So the 'optional' return disappears - return is now mandatory on
>> functions/methods, but closures don't use return (in the same way at
>> least) and the last expression in the block is the value returned to
>> the caller of the closure.
>>
>> Thoughts?
>
> The semantics related to a lexically-scoped closure is indeed that
> return will return from the method the closure is defined in. Right.
> break will gets out of the closure, or of a loop, continue gets out of
> the closure, and the loop may continue.
> I know we're not talking about implementation there, but I wonder how
> it can be done in Java? (just curious)

We'd have to use exceptions to indicate break/return/continue. Then the 
bytecode which calls the closure can catch these and do the right 
thing. This would be invisible to users calling the closure from Groovy 
code - though unfortunately it'd mean that java developers writing code 
which processes closures, would have to catch these exceptions to be 
able to support return/break/continue in a closure.

James
-------
http://radio.weblogs.com/0112098/


Gmane