jastrachan | 26 Jan 15:38 2005

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

On 26 Jan 2005, at 14:26, Russel Winder wrote:
> On Wed, 2005-01-26 at 14:18 +0000, jastrachan@... wrote:
>> String foo() {
>>      toString()
>>      return null
>> }
> Or better still:
> String foo () { toString() ; null }

Though return is mandatory (by the current proposed rules) for more 
than 1 expression.

The use case for the optional return came from wanting clean GPath 
expressions. Actually, thinking through further, the 'return' should 
not really be used anyway in a closure, as it doesn't mean 'return from 
function'. In which case, maybe things really are a whole lot simpler 
and we can then make return is mandatory in all functions/methods - its 
just not required in closures.


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.