24 Jun 2004 17:52
Re: [groovy-dev] FYI - Strange return behavior (fwd)
On 24 Jun 2004, at 15:50, Chris Poirier wrote:
> Hi John,
>
> You posted this response in the user group WRT closures using return:
>
>> The thing between { and } after the each is a closure. a closure is an
>> anonymous function. So the return in the closure returns from the
>> closure not from the enclosing method.
>>
>> This has cause people confusion before but we have yet to find a good
>> way round this.
>
> This has probably caused a lot of confusion because it's wrong!
A
> closure is /not/ an anonymous function -- it's a block a code that can
> be passed around but that maintains its scope and environment.
> "return" from a closure should most /definitely/ affect the block's
> context, not the block itself -- just like every other block. That is
> one of the reasons implicit value returns need to be supported. And,
> as has been discussed elsewhere, break/continue should also work for
> closures.
>
> Later,
> Chris.
>
> P.S. I'll post this to the JSR wiki when I get a chance.
BTW we can easily support break and continue by returning a magic
object, like Closure.BREAK - though this does mean that the caller of
the closure needs to understand this. e.g. the each(Closure) method
would need to take into account that the user may try and break /
continue. I wonder if there's some bytecode trickery we could do to
hide this.
Similarly we could do a return in a closure, which returns from the
function calling the closure - though we'd need to throw a
ReturnFromClosure(value) exception and the caller of the closure would
need to catch it & handle it nicely
James
-------
http://radio.weblogs.com/0112098/
RSS Feed