What is the meaning of return?

Alex Blewitt alex.blewitt at gmail.com
Fri Jan 29 12:12:16 PST 2010


(changing the subject slightly to focus back on this point)

On 29 Jan 2010, at 09:34, Alex Blewitt wrote:

> On 29 Jan 2010, at 06:56, "Zdenek Tronicek" <tronicek at fit.cvut.cz>  
> wrote:
>
>> You view closures as a concise notation of anonymous
>> classes and then you, naturally, expect that 'this' refers to the  
>> instance
>> of such class. "Transparency guys" view closures as blocks of code or
>> functions. Then, 'this' should refer to the enclosing instance.
>
> I believe this is a good point. If lambdas were e.g. #DEFINEs in  
> another language, then I can understand the "transparency"  
> argument.  However, I suspect that more Java developers will be  
> familiar with (anonymous) inner classes. I have no data to back this  
> up, but a comparison of widely used dynamic languages (Python, Ruby,  
> Groovy, Scala) might be a good model to follow.

At issue, I believe, is whether this code snippet prints out "Did this  
execute?"

public class JavaExample {
   pubic void method() {
     #void() {
       return;
     }.(); // or .invoke() or .do() or .wahtever()
     System.out.println("Did this execute?");
   }
}
new JavaExample().method()
// Does this print did this execute?

We can translate the above into executable snippets for other  
languages, including using Java's inner classes:

public class JavaInnerClassExample {
   public void method() [
     new Runnable() {
       public void run() { return };
     }.run();
     System.out.println("This line executes" );
   }
}
new JavaInnerClassExample().method()
// prints "This line executes"

Python (2.5):

 >>> def outer() :
...     def inner():
...             return;
...     inner()
...     print "This line executes"
...
 >>> outer()
This line executes

Scala(2.7.4):
scala> def outer() { def inner() { return; }; inner(); print("This  
line executes")}
outer():Unit
scala> outer()
This line executes

Ruby(1.6.8)
def outer()
   def inner()
     return;
   end
   inner()
   puts 'This line executes'
end
outer()
This line executes

Groovy (1.7.0)
outer = { inner = { return }; inner(); println("This line  
executes"); }; outer()
This line executes





More information about the lambda-dev mailing list