5

What's wrong with existing inner class, why inner class can't be used for representing closure?

Currently I will post here some resources to start with.

First of, below is listed great overview what is closure is. It is language agnostic. I recommend to read it Closure http://martinfowler.com/bliki/Closure.html

ADDED A BIT LATER

[January 2007] A Definition of Closures http://gafter.blogspot.com/2007/01/definition-of-closures.html

END

[December 2011] State of the Lambda http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html

[October, 2007] Advanced Topics In Programming Languages: Closures For Java http://www.youtube.com/watch?v=0zVizaCOhME

[January 4, 2011] JSR 292 goodness: Lambda to SAM type conversion using invokedynamic http://weblogs.java.net/blog/forax/archive/2011/01/04/jsr-292-goodness-lambda-sam-type-conversion-using-invokedynamic

Cœur
  • 37,241
  • 25
  • 195
  • 267
alexsmail
  • 5,661
  • 7
  • 37
  • 57
  • This is not a place to ask such general questions, OR to post general information. SO has a very specific set of guidelines, and your post violates several of them. Please read the [faq] and [ask]. – Jim Garrison Feb 13 '12 at 22:14
  • Please look on http://stackoverflow.com/questions/36636/what-is-a-closure this is far more general question. – alexsmail Feb 13 '12 at 22:19
  • 1
    @JimGarrison, I reduces the scope of the quesion. I hope now it's ok. – alexsmail Feb 13 '12 at 22:21
  • I fail to see how this question is ambiguous, vague, incomplete, overly broad, or rhethorical, and I think it can be resonably answered, as I was about to demonstrate when the question was closed. – meriton Feb 13 '12 at 22:51
  • Alright, I'll put a summary here, then: The closure semantics as defined in Brian Goetz's text differ from an anonymous class in that an anonymous inner class keeps a reference to the enclosing instance, whereas a closure doesn't. This reference can prevent garbage collection of the enclosed instance. Therefore, the equivalent to a closure would be a static inner class, which has to be named in current Java, exacerbating the syntactic overhead. – meriton Feb 14 '12 at 00:18

2 Answers2

3

As I've understood it, the plan for closures in Java 8 is just to make it less of a pain than the current workarounds with inner classes -- because seriously, the inner class syntax is a huge syntactic overhead to write simple lambdas.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • So, it is only "syntactic sugar"? :-) – alexsmail Feb 13 '12 at 22:40
  • 1
    That what I thought. Till I found `MethodHandle`. And switching implementation of the closure back and force. Now, I am confused. – alexsmail Feb 13 '12 at 22:58
  • Wow. Okay. Wow. I need to look at this some more. – Louis Wasserman Feb 13 '12 at 23:27
  • please vote to reopen this question then. :-) Thanks. :-) – alexsmail Feb 13 '12 at 23:32
  • The [Oracle doc](http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html) page has a good explanation of the various use cases for lambdas, but it seems to boil down to exactly what Louis said. Their reason is _One issue about anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, the syntax of anonymous classes may seem too unwieldy and unclear._ – michaelok Jun 20 '13 at 13:56
0

inner classes can be used to do many of the things closures can do for you.

Closures have two major advantages, they are simpler and more concise meaning they are more likely to be used. They are better understood by the language and can be optimised in ways that inner classes are not. e.g. better handling of specific exceptions, handling of primitives.

Closures can be used to build other first class methods e.g. via currying. I am not sure if this is much of an advantage but it is definitely not a simple option with nested classes.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I thought they were just syntactical sugar around inner classes (well and some rework to the typesystem), i.e. all the same limitations we have with them still apply? – Voo Feb 13 '12 at 22:42
  • Please consult the link http://weblogs.java.net/blog/forax/archive/2011/01/04/jsr-292-goodness-lambda-sam-type-conversion-using-invokedynamic above. Current suggestion _implementation_ of closures in Java is to use inner class and not MethodHandle. – alexsmail Feb 13 '12 at 22:45
  • @Voo If you starting to read the forums you will see that there are many people that said that it is _fundemantal_ change in the language and not syntactical sugar as you and I see it. Look at http://www.youtube.com/watch?v=0zVizaCOhME the link above, for example. – alexsmail Feb 13 '12 at 22:47
  • @alexsmail Thanks for the link will watch it. And sure the rework of the typesystem is a big change (and really useful for lambdas), but the closure implementation itself? Since they don't allow capture of mutable local variables it's basically the same. – Voo Feb 13 '12 at 23:02
  • 1
    @Voo You can find also this link http://gafter.blogspot.com/2007/01/definition-of-closures.html (I will add it now to the links in the question) usefull. – alexsmail Feb 13 '12 at 23:11
  • It has been suggested that inner classes be used initially, but other implementations have been suggested. Brian Geotz appeared to me to prefer a feature which the JVM understands as a closure specificity to support more interesting optimisations. e.g. In a similar way that MethodHandles can be optimised more efficiently than reflection (because the JVM understand what they are for) – Peter Lawrey Feb 14 '12 at 09:41