<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Closures for Java</title>
	<atom:link href="http://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/</link>
	<description>Thoughts, rants, and even some code from the mind of Barney Boisvert.</description>
	<lastBuildDate>Thu, 11 Sep 2014 09:58:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ricky Clarkson</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-102165</link>
		<dc:creator>Ricky Clarkson</dc:creator>
		<pubDate>Fri, 04 Jul 2008 13:56:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-102165</guid>
		<description>&lt;blockquote cite=&quot;Howard&quot;&gt;Then why can&#039;t: list.map( { (int x) =&gt; return x * 2; } ); be an inner class?&lt;/blockquote&gt;

Oh, right, so if Gafter renamed &#039;closures&#039; to &#039;inner classes&#039; but kept everything else the same, you&#039;d be happy?  I&#039;m really not sure what you mean otherwise.  By the way, the return there is unnecessary as are the parens around int x.

&lt;blockquote cite=&quot;Howard&quot;&gt;The current control structures in Java are a bit odd in that apart from :? they don&#039;t return a value (straight from C).&lt;/blockquote&gt;

Agreed, but it&#039;s generally a bit odd to have a method that has side effects also return a value.

About refactoring a closure into an inner class or vice-versa, I expect that to be automated by IDEs.  People using closures need to know that closures are blocks of code, not anonymous methods or inner classes.  If they worked like anonymous methods or inner classes they would be less useful.

&lt;blockquote cite=&quot;Howard&quot;&gt;You could write 101 Puzzlers on closures alone!&lt;/blockquote&gt;

Perhaps, but they&#039;d all be the same puzzler.

&lt;blockquote cite=&quot;Howard&quot;&gt;I think that named returns will be a lot clearer.&lt;/blockquote&gt;

Languages should not force naming.

&lt;blockquote cite=&quot;Howard&quot;&gt;But we also need to ask the question, do we really need non-local return, break, and continue?&lt;/blockquote&gt;

As long as the programmers likely to use closures are likely to think in terms of loops and side effects, return, break and continue are going to be useful.  You could argue that closures should try to force them to think functionally instead, but that&#039;s not at all what Gafter is aiming for.

&lt;blockquote cite=&quot;Howard&quot;&gt;Scala drops beak and continue completely, because of their poor interaction with closures. Also Scala is consistent about the behaviour of return throughout the language (it uses yield for example when two types or return are required).&lt;/blockquote&gt;

Scala drops break and continue because they are useless.  Scala encourages composition, not messy for loops filled with ad-hoc renamed gotos like Java encourages.  Scala has non-local returns.  Returning within a closure in Scala is the same as returning from its enclosing method.  I have obtained a NonLocalReturnException in Scala once, on purpose.  More often, you don&#039;t even use the return keyword.  I have 2268 lines of Scala, containing 16 mentions of &#039;return&#039;.</description>
		<content:encoded><![CDATA[<blockquote cite="Howard"><p>Then why can't: list.map( { (int x) =&gt; return x * 2; } ); be an inner class?</p></blockquote>
<p>Oh, right, so if Gafter renamed 'closures' to 'inner classes' but kept everything else the same, you'd be happy?  I'm really not sure what you mean otherwise.  By the way, the return there is unnecessary as are the parens around int x.</p>
<blockquote cite="Howard"><p>The current control structures in Java are a bit odd in that apart from :? they don't return a value (straight from C).</p></blockquote>
<p>Agreed, but it's generally a bit odd to have a method that has side effects also return a value.</p>
<p>About refactoring a closure into an inner class or vice-versa, I expect that to be automated by IDEs.  People using closures need to know that closures are blocks of code, not anonymous methods or inner classes.  If they worked like anonymous methods or inner classes they would be less useful.</p>
<blockquote cite="Howard"><p>You could write 101 Puzzlers on closures alone!</p></blockquote>
<p>Perhaps, but they'd all be the same puzzler.</p>
<blockquote cite="Howard"><p>I think that named returns will be a lot clearer.</p></blockquote>
<p>Languages should not force naming.</p>
<blockquote cite="Howard"><p>But we also need to ask the question, do we really need non-local return, break, and continue?</p></blockquote>
<p>As long as the programmers likely to use closures are likely to think in terms of loops and side effects, return, break and continue are going to be useful.  You could argue that closures should try to force them to think functionally instead, but that's not at all what Gafter is aiming for.</p>
<blockquote cite="Howard"><p>Scala drops beak and continue completely, because of their poor interaction with closures. Also Scala is consistent about the behaviour of return throughout the language (it uses yield for example when two types or return are required).</p></blockquote>
<p>Scala drops break and continue because they are useless.  Scala encourages composition, not messy for loops filled with ad-hoc renamed gotos like Java encourages.  Scala has non-local returns.  Returning within a closure in Scala is the same as returning from its enclosing method.  I have obtained a NonLocalReturnException in Scala once, on purpose.  More often, you don't even use the return keyword.  I have 2268 lines of Scala, containing 16 mentions of 'return'.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101670</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Thu, 03 Jul 2008 02:55:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101670</guid>
		<description>Ricky,

I, like you, am not convinced that you can do a general escape analysis to track that { [closure] }.invoke() never returns. You can probably do simple cases and Neal has added some support with type Nothing.

Your map example could be an inner class, suppose that map was:

public interface List {
public  List map( Method mapper );
public  List forEach( ( Method mapper ); // see below for forEach comments
...
}

Then why can&#039;t:

list.map( { (int x) =&gt; return x * 2; } );

be an inner class?

Also the current inner classes interpretation of return can be used like a yield statement in Scala, which is nice. EG:

List strings = asList( &quot;Hello&quot;, &quot;World&quot; );
List hStrings = strings.forEach( { (String s) =&gt; 
if( s.beginsWith( &quot;H&quot; ) ) { return s; }
return null;
} );

My forEach, in List above, only puts non-null items into its returned list, i.e. return yields the result. The current control structures in Java are a bit odd in that apart from :? they don&#039;t return a value (straight from C). This isn&#039;t usual in a functional language or even a more modern imperative language. Therefore I don&#039;t see much point in adding further support for a feature, control structures with no returned value, that is dated already.

I don&#039;t see that return as consistent in BGGA; the closure is much more like an inner class than anything else in the language, I think there will be enormous confusion when a closure is refactored into an inner class or vice versa. EG

String f() {
return new Object() { 
public toString() { 
String s = super.toString();
if ( s.endsWith( &quot;A&quot; ) ) { return &quot;A&quot;; }
return &quot;Not an A&quot;;
} } + &quot; Location&quot;;
}

and now refactor into a closure:

String f() {
return { () =&gt;
String s = super.toString();  // not the same toString
if ( s.beginsWith( &quot;A&quot; ) ) { return &quot;A&quot;; } // Doesn&#039;t return to the same place
return &quot;Not an A&quot;; // Doesn&#039;t return to the same place
} + &quot; Location&quot;; // Never actually invoked, toString will be called on the wrapped closure!
}

This will be confusing. There are numerous places were the two examples above look like they do the same thing but don&#039;t!!! Sure I picked on toString, something that all objects have hence double the confusion. But you get the idea, this will not be easy to debug with a complicated example and the compiler won&#039;t help. I think people will tend to use the two, closures and inner classes, rather interchangeably. You could write 101 Puzzlers on closures alone!

The best you can say for the closure interpretation of return is that it muddies the water. I think that named returns will be a lot clearer. But we also need to ask the question, do we really need non-local return, break, and continue? Scala drops beak and continue completely, because of their poor interaction with closures. Also Scala is consistent about the behaviour of return throughout the language (it uses yield for example when two types or return are required).</description>
		<content:encoded><![CDATA[<p>Ricky,</p>
<p>I, like you, am not convinced that you can do a general escape analysis to track that { [closure] }.invoke() never returns. You can probably do simple cases and Neal has added some support with type Nothing.</p>
<p>Your map example could be an inner class, suppose that map was:</p>
<p>public interface List {<br />
public  List map( Method mapper );<br />
public  List forEach( ( Method mapper ); // see below for forEach comments<br />
&#8230;<br />
}</p>
<p>Then why can't:</p>
<p>list.map( { (int x) =&gt; return x * 2; } );</p>
<p>be an inner class?</p>
<p>Also the current inner classes interpretation of return can be used like a yield statement in Scala, which is nice. EG:</p>
<p>List strings = asList( "Hello", "World" );<br />
List hStrings = strings.forEach( { (String s) =&gt;<br />
if( s.beginsWith( "H" ) ) { return s; }<br />
return null;<br />
} );</p>
<p>My forEach, in List above, only puts non-null items into its returned list, i.e. return yields the result. The current control structures in Java are a bit odd in that apart from :? they don't return a value (straight from C). This isn't usual in a functional language or even a more modern imperative language. Therefore I don't see much point in adding further support for a feature, control structures with no returned value, that is dated already.</p>
<p>I don't see that return as consistent in BGGA; the closure is much more like an inner class than anything else in the language, I think there will be enormous confusion when a closure is refactored into an inner class or vice versa. EG</p>
<p>String f() {<br />
return new Object() {<br />
public toString() {<br />
String s = super.toString();<br />
if ( s.endsWith( "A" ) ) { return "A"; }<br />
return "Not an A";<br />
} } + " Location";<br />
}</p>
<p>and now refactor into a closure:</p>
<p>String f() {<br />
return { () =&gt;<br />
String s = super.toString();  // not the same toString<br />
if ( s.beginsWith( "A" ) ) { return "A"; } // Doesn't return to the same place<br />
return "Not an A"; // Doesn't return to the same place<br />
} + " Location"; // Never actually invoked, toString will be called on the wrapped closure!<br />
}</p>
<p>This will be confusing. There are numerous places were the two examples above look like they do the same thing but don't!!! Sure I picked on toString, something that all objects have hence double the confusion. But you get the idea, this will not be easy to debug with a complicated example and the compiler won't help. I think people will tend to use the two, closures and inner classes, rather interchangeably. You could write 101 Puzzlers on closures alone!</p>
<p>The best you can say for the closure interpretation of return is that it muddies the water. I think that named returns will be a lot clearer. But we also need to ask the question, do we really need non-local return, break, and continue? Scala drops beak and continue completely, because of their poor interaction with closures. Also Scala is consistent about the behaviour of return throughout the language (it uses yield for example when two types or return are required).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ricky Clarkson</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101645</link>
		<dc:creator>Ricky Clarkson</dc:creator>
		<pubDate>Thu, 03 Jul 2008 01:13:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101645</guid>
		<description>&quot;I can&#039;t think of an example where you couldn&#039;t use an inner class with short syntax where you can use a closure.&quot;

Anywhere where you return, use this, write to local variables from the enclosing scope, continue or break is awkward to use an inner class with or without short syntax.

Anywhere where you don&#039;t care to see the types involved because they aren&#039;t interesting is annoying to use an inner class.  list.map({ int x =&gt; x*2 }) is clearer than list.map(new IntToIntFunction(){ public int invoke(int x) { return x*2; } } ).  Note that clear doesn&#039;t mean &#039;more explicit&#039;.

The problem you mentioned is about tracing whether code returns or not, and isn&#039;t fundamentally about the syntax.  If the closure { =&gt; return 5; } were tagged by the compiler as always returning, then the method that invokes it were detected to always invoke it, it would be possible to guarantee that the return happens, and the compile error you mentioned would disappear.  In practice a big use case of closures is to pass them to other methods, so you&#039;d lose this information quite quickly.

I look forward to seeing a real case where this is a problem.

&quot;Re. non-local return: If you want this feature just add it to the language in general including inner classes, this way the whole language remains consistent - not different behaviour inside a closure to the rest.&quot;

It&#039;s not different behaviour inside a closure to the rest, it&#039;s identical.  Return returns from the method it&#039;s used in.  This does not change when it&#039;s inside a closure as a closure is not a method.

Barney, sorry if this conversation is considered noise.</description>
		<content:encoded><![CDATA[<p>"I can't think of an example where you couldn't use an inner class with short syntax where you can use a closure."</p>
<p>Anywhere where you return, use this, write to local variables from the enclosing scope, continue or break is awkward to use an inner class with or without short syntax.</p>
<p>Anywhere where you don't care to see the types involved because they aren't interesting is annoying to use an inner class.  list.map({ int x =&gt; x*2 }) is clearer than list.map(new IntToIntFunction(){ public int invoke(int x) { return x*2; } } ).  Note that clear doesn't mean 'more explicit'.</p>
<p>The problem you mentioned is about tracing whether code returns or not, and isn't fundamentally about the syntax.  If the closure { =&gt; return 5; } were tagged by the compiler as always returning, then the method that invokes it were detected to always invoke it, it would be possible to guarantee that the return happens, and the compile error you mentioned would disappear.  In practice a big use case of closures is to pass them to other methods, so you'd lose this information quite quickly.</p>
<p>I look forward to seeing a real case where this is a problem.</p>
<p>"Re. non-local return: If you want this feature just add it to the language in general including inner classes, this way the whole language remains consistent &#8211; not different behaviour inside a closure to the rest."</p>
<p>It's not different behaviour inside a closure to the rest, it's identical.  Return returns from the method it's used in.  This does not change when it's inside a closure as a closure is not a method.</p>
<p>Barney, sorry if this conversation is considered noise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101641</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Thu, 03 Jul 2008 00:43:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101641</guid>
		<description>Ricky,

Re. AbstractAction: I disagree, there are plenty of useful partial implementations that have more than one method that is potentially overridable.

Re. usefulness of closures: I can&#039;t think of an example where you couldn&#039;t use an inner class with short syntax where you can use a closure. The keys are short syntax, write to locals, and possibly non-local return, break, and continue - see below. I have nothing against improving inner classes with short syntax etc.

Re. Tennant: The closures proposal breaks Tennant, consider:

int f() { return 1; }

now lets wrap it:

int f() { 
{ () =&gt; return 1; }.invoke(); 
// Syntax error here - doesn&#039;t return an int
}

Oops syntax error before the final }, since it doesn&#039;t return an int. Tennant uses this argument to argue against the return, break, and continue statements in his book. To sum up, Tennant himself rejects the return statement as being compatible with his principle. Hence closures don&#039;t give you Tennant&#039;s principle.

Re. non-local return: If you want this feature just add it to the language in general including inner classes, this way the whole language remains consistent - not different behaviour inside a closure to the rest. Using the BGGA syntax for an inner class with a named return:

String f() { 
forEach( 
new String[] { &quot;hello&quot;, &quot;world&quot; }, 
{ (String s) =&gt; if ( s.startsWith( &quot;h&quot; ) ) { f.return s; } } 
);
}

As you can see, exactly the same with an inner class as with a closure using named return. As I said before I think Neal is considering named returns for BGGA since the current syntax for the multiple types of return is confusing and hence I used named return above. I should spell this out; if you decide that non-local return is a good idea then add it to the whole language, not just the latest bolt on goody. Be consistent.</description>
		<content:encoded><![CDATA[<p>Ricky,</p>
<p>Re. AbstractAction: I disagree, there are plenty of useful partial implementations that have more than one method that is potentially overridable.</p>
<p>Re. usefulness of closures: I can't think of an example where you couldn't use an inner class with short syntax where you can use a closure. The keys are short syntax, write to locals, and possibly non-local return, break, and continue &#8211; see below. I have nothing against improving inner classes with short syntax etc.</p>
<p>Re. Tennant: The closures proposal breaks Tennant, consider:</p>
<p>int f() { return 1; }</p>
<p>now lets wrap it:</p>
<p>int f() {<br />
{ () =&gt; return 1; }.invoke();<br />
// Syntax error here &#8211; doesn't return an int<br />
}</p>
<p>Oops syntax error before the final }, since it doesn't return an int. Tennant uses this argument to argue against the return, break, and continue statements in his book. To sum up, Tennant himself rejects the return statement as being compatible with his principle. Hence closures don't give you Tennant's principle.</p>
<p>Re. non-local return: If you want this feature just add it to the language in general including inner classes, this way the whole language remains consistent &#8211; not different behaviour inside a closure to the rest. Using the BGGA syntax for an inner class with a named return:</p>
<p>String f() {<br />
forEach(<br />
new String[] { "hello", "world" },<br />
{ (String s) =&gt; if ( s.startsWith( "h" ) ) { f.return s; } }<br />
);<br />
}</p>
<p>As you can see, exactly the same with an inner class as with a closure using named return. As I said before I think Neal is considering named returns for BGGA since the current syntax for the multiple types of return is confusing and hence I used named return above. I should spell this out; if you decide that non-local return is a good idea then add it to the whole language, not just the latest bolt on goody. Be consistent.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ricky Clarkson</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101442</link>
		<dc:creator>Ricky Clarkson</dc:creator>
		<pubDate>Wed, 02 Jul 2008 09:13:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101442</guid>
		<description>Howard,

That is a fault with the AbstractAction type.  It should be a one-method interface, or should be renamed to NotAbstractEnough :).  You can easily create a one-method interface and a wrapper around AbstractAction without losing anything.

I agree about putting the field in the outer scope, but I&#039;d rather not have the field if I can manage that.

You&#039;re not getting the point though.  Closures are useful where anonymous classes are ridiculous.  They are not a complete replacement.

&quot;What is better about a closure?&quot;  Readability and &#039;wrapability&#039;.  Wrapability is what Gafter was talking about with his mentions of Tennent&#039;s Correspondence Principle.  code should have the same meaning as { =&gt; code}.invoke(), in all cases.  Wrapping code into an anonymous class makes it have a different meaning than it had before, making you have to do awkward things.  Consider changing:

for (String s: new String[]{&quot;hello&quot;, &quot;world&quot;})
    if (s.startsWith(&quot;h&quot;))
        return s;

into a call to a forEach method that takes a closure/anonymous class.  Using an anonymous class, and a simple method signature for forEach (void forEach(Action action);), the caller would have to throw an exception from inside the anonymous class and catch it from outside to achieve the early return.  It&#039;s a similar story with break, continue, this and writing to local variables from the enclosing scope.

If you have a problem with forEach as an example, because there&#039;s already foreach in the language, think of another example.</description>
		<content:encoded><![CDATA[<p>Howard,</p>
<p>That is a fault with the AbstractAction type.  It should be a one-method interface, or should be renamed to NotAbstractEnough :).  You can easily create a one-method interface and a wrapper around AbstractAction without losing anything.</p>
<p>I agree about putting the field in the outer scope, but I'd rather not have the field if I can manage that.</p>
<p>You're not getting the point though.  Closures are useful where anonymous classes are ridiculous.  They are not a complete replacement.</p>
<p>"What is better about a closure?"  Readability and 'wrapability'.  Wrapability is what Gafter was talking about with his mentions of Tennent's Correspondence Principle.  code should have the same meaning as { =&gt; code}.invoke(), in all cases.  Wrapping code into an anonymous class makes it have a different meaning than it had before, making you have to do awkward things.  Consider changing:</p>
<p>for (String s: new String[]{"hello", "world"})<br />
    if (s.startsWith("h"))<br />
        return s;</p>
<p>into a call to a forEach method that takes a closure/anonymous class.  Using an anonymous class, and a simple method signature for forEach (void forEach(Action action);), the caller would have to throw an exception from inside the anonymous class and catch it from outside to achieve the early return.  It's a similar story with break, continue, this and writing to local variables from the enclosing scope.</p>
<p>If you have a problem with forEach as an example, because there's already foreach in the language, think of another example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101439</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Wed, 02 Jul 2008 08:59:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101439</guid>
		<description>Ricky, Suppose you want to implement an AbstractAction for an event, easy with an inner class hard with a closure. Putting the field in the outer scope isn&#039;t ideal, you increase the visibility and allow errors. Plenty of things that have one public entry point have numerous private methods, easy with an inner class hard with a closure. The list goes on. Anyway, turn the question round - what is better about a closure? Howard.</description>
		<content:encoded><![CDATA[<p>Ricky, Suppose you want to implement an AbstractAction for an event, easy with an inner class hard with a closure. Putting the field in the outer scope isn't ideal, you increase the visibility and allow errors. Plenty of things that have one public entry point have numerous private methods, easy with an inner class hard with a closure. The list goes on. Anyway, turn the question round &#8211; what is better about a closure? Howard.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: icubetech</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-101243</link>
		<dc:creator>icubetech</dc:creator>
		<pubDate>Tue, 01 Jul 2008 22:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-101243</guid>
		<description>lol and perhaps you rediscover soon functors , lambda calculus and strong typing can reduce many of your complexity problems mdr with strong rules for coding ,that  xml can be easily map top functunal languages ... you trying to add an extender pattern with functional languages facilities in j7  ;)

jfeven aka java fonctors 7even 

ps : Now i know why i hate java!proof of program !!!</description>
		<content:encoded><![CDATA[<p>lol and perhaps you rediscover soon functors , lambda calculus and strong typing can reduce many of your complexity problems mdr with strong rules for coding ,that  xml can be easily map top functunal languages &#8230; you trying to add an extender pattern with functional languages facilities in j7  ;)</p>
<p>jfeven aka java fonctors 7even </p>
<p>ps : Now i know why i hate java!proof of program !!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ricky Clarkson</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-100963</link>
		<dc:creator>Ricky Clarkson</dc:creator>
		<pubDate>Mon, 30 Jun 2008 22:36:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-100963</guid>
		<description>Howard,

A closure has two this pointers in the implementation, but you only see one from code.  I&#039;m not sure why you&#039;d need a pointer to an object that has nothing more than a single method, from within that method, other than for recursion, which I&#039;ll soon address.

Saying that closures are a subset of inner classes is akin to saying that for loops are a subset of gotos.  While it&#039;s technically true, it&#039;s not a very meaningful thing to say.

&quot;There are a whole host of things you can do with an inner class that you can&#039;t do with a closure: inherit implementations&quot;

It&#039;s a single method/function - how would it benefit from inheritance?

..&quot;fields&quot; - writing a closure that has a field is the same as writing a closure that uses a local variable from the enclosing scope, in terms of what you can do with it.

..&quot;more than one method&quot; - sure, you&#039;re talking about writing a class here, not about a block of code.  Cars aren&#039;t appropriate for crossing bodies of water.

..&quot;recursion&quot;.  I once showed this Java code on a Java IRC channel, only to be told it wasn&#039;t Java:

System.out.println(new Object(){public int fac(int x) { return x&lt;2 ? x : x*fac(x-1); } }.fac(5));

In other words, the closest you can do to a recursive lambda in Java is code that many people won&#039;t understand anyway.  I think you can write a combinator to write a recursive lambda using the BGGA prototype, but I haven&#039;t quite understood how yet.  The usual combinators you can use in lisp don&#039;t translate to typed languages particularly well.</description>
		<content:encoded><![CDATA[<p>Howard,</p>
<p>A closure has two this pointers in the implementation, but you only see one from code.  I'm not sure why you'd need a pointer to an object that has nothing more than a single method, from within that method, other than for recursion, which I'll soon address.</p>
<p>Saying that closures are a subset of inner classes is akin to saying that for loops are a subset of gotos.  While it's technically true, it's not a very meaningful thing to say.</p>
<p>"There are a whole host of things you can do with an inner class that you can't do with a closure: inherit implementations"</p>
<p>It's a single method/function &#8211; how would it benefit from inheritance?</p>
<p>.."fields" &#8211; writing a closure that has a field is the same as writing a closure that uses a local variable from the enclosing scope, in terms of what you can do with it.</p>
<p>.."more than one method" &#8211; sure, you're talking about writing a class here, not about a block of code.  Cars aren't appropriate for crossing bodies of water.</p>
<p>.."recursion".  I once showed this Java code on a Java IRC channel, only to be told it wasn't Java:</p>
<p>System.out.println(new Object(){public int fac(int x) { return x&lt;2 ? x : x*fac(x-1); } }.fac(5));</p>
<p>In other words, the closest you can do to a recursive lambda in Java is code that many people won't understand anyway.  I think you can write a combinator to write a recursive lambda using the BGGA prototype, but I haven't quite understood how yet.  The usual combinators you can use in lisp don't translate to typed languages particularly well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Easter</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-100892</link>
		<dc:creator>Michael Easter</dc:creator>
		<pubDate>Mon, 30 Jun 2008 14:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-100892</guid>
		<description>The original post talks about &#039;reading&#039; but not &#039;coding&#039; or &#039;prototyping&#039;.

A prototype is available: try it out! Like generics, the syntax is harsh at first but it becomes clear. The BGGA spec is actually quite elegant in many ways, and very powerful. You may or may not agree on elegance but you&#039;ll have more credibility after giving it an honest test-drive.</description>
		<content:encoded><![CDATA[<p>The original post talks about 'reading' but not 'coding' or 'prototyping'.</p>
<p>A prototype is available: try it out! Like generics, the syntax is harsh at first but it becomes clear. The BGGA spec is actually quite elegant in many ways, and very powerful. You may or may not agree on elegance but you'll have more credibility after giving it an honest test-drive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Howard Lovatt</title>
		<link>https://www.barneyb.com/barneyblog/2008/06/26/closures-for-java/comment-page-1/#comment-100839</link>
		<dc:creator>Howard Lovatt</dc:creator>
		<pubDate>Mon, 30 Jun 2008 08:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=431#comment-100839</guid>
		<description>Hi Ricky, 

The difference is that the closure has one this pointer, it points to the enclosing class. An inner class has two this pointers, one that points to the inherited class and one that points to the enclosing class. Therefore a closure is, in a strict mathematical sense, not in a my opinion sense, a subset of an inner class. There are a whole host of things you can do with an inner class that you can&#039;t do with a closure: inherit implementations, fields, more than one method, recursion, etc. Some examples are here: &lt;a href=&quot;http://www.artima.com/weblogs/viewpost.jsp?thread=202004&quot; rel=&quot;nofollow&quot;&gt; http://www.artima.com/weblogs/viewpost.jsp?thread=202004&lt;/a&gt;.

Turn the question round, what can a closure do that an inner class can&#039;t. You will soon see that it is a subset of an inner class.

Howard.</description>
		<content:encoded><![CDATA[<p>Hi Ricky, </p>
<p>The difference is that the closure has one this pointer, it points to the enclosing class. An inner class has two this pointers, one that points to the inherited class and one that points to the enclosing class. Therefore a closure is, in a strict mathematical sense, not in a my opinion sense, a subset of an inner class. There are a whole host of things you can do with an inner class that you can't do with a closure: inherit implementations, fields, more than one method, recursion, etc. Some examples are here: <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=202004" rel="nofollow"> </a><a href="http://www.artima.com/weblogs/viewpost.jsp?thread=202004" rel="nofollow">http://www.artima.com/weblogs/viewpost.jsp?thread=202004</a>.</p>
<p>Turn the question round, what can a closure do that an inner class can't. You will soon see that it is a subset of an inner class.</p>
<p>Howard.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
