J
Jon Harrop
I've been hearing about Java's anonymous inner classes a lot recently.
What's the difference between these and closures?
What's the difference between these and closures?
See: http://martinfowler.com/bliki/Closure.htmlI've been hearing about Java's anonymous inner classes a lot recently.
What's the difference between these and closures?
Jon said:I've been hearing about Java's anonymous inner classes a lot recently.
What's the difference between these and closures?
Tom said:Briefly, anonymous inner classes:
* have a hideously over-verbose syntax
* cannot assign local variables (use non-finals)
* cannot break/continue/return through to outer context
* 'this' references the wrong object
* may have multiple methods and instance fields
* can subtype interfaces and classes
In a perfect language you would be able to take a block of code and wrap
it as a closure without changing any of the text of the block itself.
For my money, it's worth fixing the first two points, and warning on an
unqualified this.
Tom said:Briefly, anonymous inner classes:
* have a hideously over-verbose syntax
* cannot assign local variables (use non-finals)
* cannot break/continue/return through to outer context
* 'this' references the wrong object
* may have multiple methods and instance fields
* can subtype interfaces and classes
In a perfect language you would be able to take a block of code and wrap
it as a closure without changing any of the text of the block itself.
Jon Harrop said:Ok, so they really screwed this one up. :-(
Anonymous inner classes are not closures, correct; but they solve a
need that java has for simple objects that work within the type
system. They're very useful for what they do.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.