Fwd: Question on return in a closure

Neal Gafter neal at gafter.com
Tue May 13 10:32:04 PDT 2008


Forwarded FYI

---------- Forwarded message ----------
From: Neal Gafter
Date: Tue, May 13, 2008 at 10:30 AM
Subject: Re: Question on return in a closure
To: Erik Hellman


Erik-

Your code is not syntactically correct, as the if-then-else statement
requires the controlled parts to be statements.  In the else part you have
"Simple String;", but that is not a legal statement.  I think the code
you're asking about should be written

public class SimpleClass {

   public void simpleMethod(boolean doReturn) {
     String value = { ==>
         if(doReturn)
           return;
         "Simple String"
      }.invoke();
      System.out.println(value);
   }
}

The nonlocal return is only legal if you write the closure with the long
arrow '==>'; see http://gafter.blogspot.com/2007/12/restricted-closures.html

In this case the closure's result type is String.  Now, simpleMethod(false)
will print "Simple String", and simpleMethod(true) will return to the caller
without printing anything.  In fact, it does the same thing as this:

public class SimpleClass {

   public void simpleMethod(boolean doReturn) {
      if(doReturn)
         return;
      String value = "Simple String";
      System.out.println(value);
   }
}

Regards,
Neal


On Tue, May 13, 2008 at 9:51 AM, Erik Hellman wrote:

> Hi Neal!
>
> I've been following the debate around closures for Java for some time now
> and find it all very interresting. Recently I was invited by the Swedish
> Java Usergroup (JavaForum Sweden) to give a introduction on closures for
> Java in general. I'm putting together a few slides with a couple of demos
> and I realised that I'm a little uncertain about a detail concerning return
> inside a closure and I hope that you would have the time to answer it (if
> not, I'll simply exclude that example from the presentation :).
>
> Anyway, in one of your blog post (Tennent's Correspondence Principle and
> Returning From a Closure,
> http://gafter.blogspot.com/2006/08/tennents-correspondence-principle-and.html)
> you explain why return won't return the closure but the nearest enclosing
> method. However, what happens if the expression within the closure can
> either call return or result in an expression of a certain type (like a
> String). Example:
>
> public class SimpleClass {
>
>    public void simpleMethod(boolean doReturn) {
>       String value = { =>
>          if(doReturn)
>             return;
>          else
>             "Simple String";
>       }.invoke();
>
>       System.out.println(value);
>    }
> }
>
> Would this be valid? I feel a bit confused about this since I can't find a
> reason for it not to work, but it doesn't feel right that the closure can
> result in two different types (void or String).
>
> Hope you can help.
>
> Thanks!
>
> // Erik Hellman
>  *public class SimpleClass { *
> *   *
> *   public void simpleMethod(boolean doReturn) { *
> *      String value = { => *
> *         if(doReturn) *
> *            return; *
> *         else *
> *            "Simple String"; *
> *      }.invoke(); *
> * *
> *      System.out.println(value); *
> *   } *
> *} *
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20080513/52ea4832/attachment.html 


More information about the closures-dev mailing list