<?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: D doesn&#8217;t have real closures</title>
	<atom:link href="http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/</link>
	<description>Hans-Eric Grönlund on software development</description>
	<lastBuildDate>Mon, 22 Feb 2010 09:32:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: The Functional Subset of D</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-8048</link>
		<dc:creator>The Functional Subset of D</dc:creator>
		<pubDate>Thu, 22 May 2008 09:22:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-8048</guid>
		<description>[...] Note that the extracted function f (referenced by the a_closure variable) accesses the local variable x, although it has gone out of scope at the time of execution. D got this ability with the 2.07 version, before that it didn&#8217;t have real closures. [...]</description>
		<content:encoded><![CDATA[<p>[...] Note that the extracted function f (referenced by the a_closure variable) accesses the local variable x, although it has gone out of scope at the time of execution. D got this ability with the 2.07 version, before that it didn&#8217;t have real closures. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vladimir</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-551</link>
		<dc:creator>Vladimir</dc:creator>
		<pubDate>Fri, 02 Nov 2007 22:28:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-551</guid>
		<description>D 2.007 adds &quot;full closure support&quot; (see link for changelog).</description>
		<content:encoded><![CDATA[<p>D 2.007 adds &#8220;full closure support&#8221; (see link for changelog).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Foo</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-217</link>
		<dc:creator>Foo</dc:creator>
		<pubDate>Thu, 13 Sep 2007 17:56:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-217</guid>
		<description>I&#039;m back, Joshua!

Joshua wrote:
&gt; Bookmarked, ’cause I want to know how you use array to get closures in Java (come back, Foo, come back!). Not that I actually write in Java hardly ever, but still.

Answer: Java&#039;s anonymous functions are *almost* closures.  Specifically, when you build an anonymous function within a block, it&#039;s closed to all final local variables declared in that block or in an outer block.

The problem is that those variables *must* be final.  Why?  Who knows?  There&#039;s no performance hit unless you actually use a non-final variable, and even that&#039;s not a very big hit.  But Sun sought to screw us over I guess.

Well, almost.  The array trick is as follows.  Let&#039;s say I wanted to make an accumulator generator (Paul Graham style, albeit with methods).  I can do this:

interface NextInt { public int next(); }

public void makeNextInt(int init)
{
final int[] count = { init };
return new NextInt() { public int next() { return ++count[0]; }
}

Note that the NextInt class uses an outer lexical variable (count), and thus that variable must be final.  But it doesn&#039;t mean you can&#039;t change it -- it just has to be composite.  Instead of having it be an int, make it be an int[1].  That&#039;s the array trick.

Of course, you can make a MutableInteger or whatever if you want, and that&#039;s a little faster as it doesn&#039;t require an array bounds check, but it&#039;s more work to write.  It&#039;s not part of the syntax of the language like the array is.

It turns out that there was a strong counterproposal to the crap being put out right now for excessively complex closure semantics.  The counterproposal just does a simple semantic wrapper around exactly this idea.  Let&#039;s hope the Java folks get a clue and adopt it.

RenoX asked: 

&gt; Well Foo as the author has shown, D has anonymous function so you can use them for GUIs, are closure really necessary for GUIs?

Closures are hardly a necessary feature.  But they are an *enormously* valuable feature for GUIs, threading, event management, and exception handling.  Because they make doing callbacks elegant.  Having anonymous functions without having closures based on lack of &quot;need&quot; is like saying you can do everything in recursion, so why bother with including for-loops...</description>
		<content:encoded><![CDATA[<p>I&#8217;m back, Joshua!</p>
<p>Joshua wrote:<br />
&gt; Bookmarked, ’cause I want to know how you use array to get closures in Java (come back, Foo, come back!). Not that I actually write in Java hardly ever, but still.</p>
<p>Answer: Java&#8217;s anonymous functions are *almost* closures.  Specifically, when you build an anonymous function within a block, it&#8217;s closed to all final local variables declared in that block or in an outer block.</p>
<p>The problem is that those variables *must* be final.  Why?  Who knows?  There&#8217;s no performance hit unless you actually use a non-final variable, and even that&#8217;s not a very big hit.  But Sun sought to screw us over I guess.</p>
<p>Well, almost.  The array trick is as follows.  Let&#8217;s say I wanted to make an accumulator generator (Paul Graham style, albeit with methods).  I can do this:</p>
<p>interface NextInt { public int next(); }</p>
<p>public void makeNextInt(int init)<br />
{<br />
final int[] count = { init };<br />
return new NextInt() { public int next() { return ++count[0]; }<br />
}</p>
<p>Note that the NextInt class uses an outer lexical variable (count), and thus that variable must be final.  But it doesn&#8217;t mean you can&#8217;t change it &#8212; it just has to be composite.  Instead of having it be an int, make it be an int[1].  That&#8217;s the array trick.</p>
<p>Of course, you can make a MutableInteger or whatever if you want, and that&#8217;s a little faster as it doesn&#8217;t require an array bounds check, but it&#8217;s more work to write.  It&#8217;s not part of the syntax of the language like the array is.</p>
<p>It turns out that there was a strong counterproposal to the crap being put out right now for excessively complex closure semantics.  The counterproposal just does a simple semantic wrapper around exactly this idea.  Let&#8217;s hope the Java folks get a clue and adopt it.</p>
<p>RenoX asked: </p>
<p>&gt; Well Foo as the author has shown, D has anonymous function so you can use them for GUIs, are closure really necessary for GUIs?</p>
<p>Closures are hardly a necessary feature.  But they are an *enormously* valuable feature for GUIs, threading, event management, and exception handling.  Because they make doing callbacks elegant.  Having anonymous functions without having closures based on lack of &#8220;need&#8221; is like saying you can do everything in recursion, so why bother with including for-loops&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zinf</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-212</link>
		<dc:creator>zinf</dc:creator>
		<pubDate>Thu, 13 Sep 2007 13:41:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-212</guid>
		<description>the array trick is to use a single element final array to store a non final value. this way you can still modify it from an inner class. i&#039;ve used it for test classes, but i hope to never have to use it for anything else... hopeful gafter will have his way and get closures into java 7.

Alex</description>
		<content:encoded><![CDATA[<p>the array trick is to use a single element final array to store a non final value. this way you can still modify it from an inner class. i&#8217;ve used it for test classes, but i hope to never have to use it for anything else&#8230; hopeful gafter will have his way and get closures into java 7.</p>
<p>Alex</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans-Eric</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-210</link>
		<dc:creator>Hans-Eric</dc:creator>
		<pubDate>Thu, 13 Sep 2007 11:45:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-210</guid>
		<description>Bruno: In C# a delegate can be assigned to a static method and a non-static method interchangeably. You can even mix static and non-static methods within the same multi cast delegate.
D only allows non-static (instance) methods.

FeepingCreature: Three interesting ways to fake closures, thanks for the tip.

Daniel: Your stack-copying writeup is really cool, I had no idea it could be that simple.

Joshua: You have several good points in your comments (and no bad ones :-) ).</description>
		<content:encoded><![CDATA[<p>Bruno: In C# a delegate can be assigned to a static method and a non-static method interchangeably. You can even mix static and non-static methods within the same multi cast delegate.<br />
D only allows non-static (instance) methods.</p>
<p>FeepingCreature: Three interesting ways to fake closures, thanks for the tip.</p>
<p>Daniel: Your stack-copying writeup is really cool, I had no idea it could be that simple.</p>
<p>Joshua: You have several good points in your comments (and no bad ones <img src='http://www.hans-eric.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: www.hans-eric.com &#187; Blog Archive &#187; Should D have real closures?</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-209</link>
		<dc:creator>www.hans-eric.com &#187; Blog Archive &#187; Should D have real closures?</dc:creator>
		<pubDate>Thu, 13 Sep 2007 08:38:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-209</guid>
		<description>[...] has been a vivid debate following my D doesn&#8217;t have real closures post. For most parts it has been a constructive discussion, but what I wanted to see was real [...]</description>
		<content:encoded><![CDATA[<p>[...] has been a vivid debate following my D doesn&#8217;t have real closures post. For most parts it has been a constructive discussion, but what I wanted to see was real [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julio César Carrascal Urquijo &#187; Archivo &#187; D should have real closures.</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-208</link>
		<dc:creator>Julio César Carrascal Urquijo &#187; Archivo &#187; D should have real closures.</dc:creator>
		<pubDate>Thu, 13 Sep 2007 05:01:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-208</guid>
		<description>[...] post is a response to Hans Eric&#8217;s post on &#8220;D doesn’t have real closures&#8220;. I recommend you read the article and comments before reading [...]</description>
		<content:encoded><![CDATA[<p>[...] post is a response to Hans Eric&#8217;s post on &#8220;D doesn’t have real closures&#8220;. I recommend you read the article and comments before reading [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruno Medeiros</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-206</link>
		<dc:creator>Bruno Medeiros</dc:creator>
		<pubDate>Wed, 12 Sep 2007 20:10:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-206</guid>
		<description>What  does it mean that &quot;For instance, C# delegates may bind to both static and non-static methods, while D may bind to non-static methods only. &quot; ? That doesn&#039;t seem to make sense.</description>
		<content:encoded><![CDATA[<p>What  does it mean that &#8220;For instance, C# delegates may bind to both static and non-static methods, while D may bind to non-static methods only. &#8221; ? That doesn&#8217;t seem to make sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The One With D &#187; Blog Archive &#187; On Closures in D</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-203</link>
		<dc:creator>The One With D &#187; Blog Archive &#187; On Closures in D</dc:creator>
		<pubDate>Wed, 12 Sep 2007 06:51:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-203</guid>
		<description>[...] news day for D, but there&#8217;s a blog post over at hans-eric.com titled, D doesn&#8217;t have real closures. Hans-Eric talks about D&#8217;s delegates, how they are similar to closures, and how they [...]</description>
		<content:encoded><![CDATA[<p>[...] news day for D, but there&#8217;s a blog post over at hans-eric.com titled, D doesn&#8217;t have real closures. Hans-Eric talks about D&#8217;s delegates, how they are similar to closures, and how they [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/comment-page-1/#comment-202</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Wed, 12 Sep 2007 06:21:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-202</guid>
		<description>Coincidentally, I posted details of the stack-copying technique FeepingCreature talked about a few weeks ago over at: http://while-nan.blogspot.com/2007/08/fakin-closures.html

To be honest, it&#039;s fairly rare that I come across a case where I miss being able to just directly return an inner function; and in cases where I want to do so, D has a few ways of working around that (both the hack in my post above, and also ones involving constructs like bind).</description>
		<content:encoded><![CDATA[<p>Coincidentally, I posted details of the stack-copying technique FeepingCreature talked about a few weeks ago over at: <a href="http://while-nan.blogspot.com/2007/08/fakin-closures.html" rel="nofollow">http://while-nan.blogspot.com/2007/08/fakin-closures.html</a></p>
<p>To be honest, it&#8217;s fairly rare that I come across a case where I miss being able to just directly return an inner function; and in cases where I want to do so, D has a few ways of working around that (both the hack in my post above, and also ones involving constructs like bind).</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.372 seconds -->
