<?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"
	>
<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>
	<pubDate>Sat, 17 May 2008 00:23:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Vladimir</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#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 "full closure support" (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-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'm back, Joshua!

Joshua wrote:
&#62; 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's anonymous functions are *almost* closures.  Specifically, when you build an anonymous function within a block, it'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's no performance hit unless you actually use a non-final variable, and even that's not a very big hit.  But Sun sought to screw us over I guess.

Well, almost.  The array trick is as follows.  Let'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't mean you can't change it -- it just has to be composite.  Instead of having it be an int, make it be an int[1].  That's the array trick.

Of course, you can make a MutableInteger or whatever if you want, and that's a little faster as it doesn't require an array bounds check, but it's more work to write.  It'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's hope the Java folks get a clue and adopt it.

RenoX asked: 

&#62; 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 "need" 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-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'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-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-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-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-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 "For instance, C# delegates may bind to both static and non-static methods, while D may bind to non-static methods only. " ? That doesn'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-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-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'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>
	<item>
		<title>By: renoX</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-201</link>
		<dc:creator>renoX</dc:creator>
		<pubDate>Wed, 12 Sep 2007 05:15:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-201</guid>
		<description>This blog post  was discussed  in the D.announce newsgroup 
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.announce&#38;article_id=9874

Here the code from Jarrett Billingsley for the multi-cast delegates:
&#62;&#62;
struct MCD(Args...)
{
    void function(Args)[] funcs;

    void opCatAssign(void function(Args) f)
    {
        funcs ~= f;
    }

    void opCall(Args args)
    {
        foreach(f; funcs)
            f(args);
    }
}

void fa(int x)
{
    Stdout.formatln("fa({})", x);
}

void fb(int x)
{
    Stdout.formatln("fb({})", x);
}

void main()
{
    MCD!(int) a;
    a ~= &fa;
    a ~= &fb;
    a(3);
}
</description>
		<content:encoded><![CDATA[<p>This blog post  was discussed  in the D.announce newsgroup<br />
<a href="http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.announce&amp;article_id=9874" rel="nofollow">http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.announce&amp;article_id=9874</a></p>
<p>Here the code from Jarrett Billingsley for the multi-cast delegates:<br />
&gt;&gt;<br />
struct MCD(Args&#8230;)<br />
{<br />
    void function(Args)[] funcs;</p>
<p>    void opCatAssign(void function(Args) f)<br />
    {<br />
        funcs ~= f;<br />
    }</p>
<p>    void opCall(Args args)<br />
    {<br />
        foreach(f; funcs)<br />
            f(args);<br />
    }<br />
}</p>
<p>void fa(int x)<br />
{<br />
    Stdout.formatln(&#8221;fa({})&#8221;, x);<br />
}</p>
<p>void fb(int x)<br />
{<br />
    Stdout.formatln(&#8221;fb({})&#8221;, x);<br />
}</p>
<p>void main()<br />
{<br />
    MCD!(int) a;<br />
    a ~= &fa;<br />
    a ~= &fb;<br />
    a(3);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FeepingCreature</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-197</link>
		<dc:creator>FeepingCreature</dc:creator>
		<pubDate>Tue, 11 Sep 2007 20:08:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-197</guid>
		<description>Um
Point the first: You can emulate closures in D with just a little bit of platform dependent code by writing a function that copies the stack. This is the easiest way - like "return stacklift(&#38;countdown_helper); "
Point the second: You can also emulate closures in D by binding a function with the variables it will use. Example:
int countdown_helper(int c) { return c--; }
return bind(&#38;countdown_helper, current);
Point the third: As you said yourself, if you're in a hurry, why not just use inner classes/structs?
struct countdown_helper { int c; int call() { return c--; } }
return &#38;(new countdown_helper).call;

Point being, there's lots of ways to get close to real closures in D :)</description>
		<content:encoded><![CDATA[<p>Um<br />
Point the first: You can emulate closures in D with just a little bit of platform dependent code by writing a function that copies the stack. This is the easiest way - like &#8220;return stacklift(&amp;countdown_helper); &#8221;<br />
Point the second: You can also emulate closures in D by binding a function with the variables it will use. Example:<br />
int countdown_helper(int c) { return c&#8211;; }<br />
return bind(&amp;countdown_helper, current);<br />
Point the third: As you said yourself, if you&#8217;re in a hurry, why not just use inner classes/structs?<br />
struct countdown_helper { int c; int call() { return c&#8211;; } }<br />
return &amp;(new countdown_helper).call;</p>
<p>Point being, there&#8217;s lots of ways to get close to real closures in D <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: Joshua Paine</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-196</link>
		<dc:creator>Joshua Paine</dc:creator>
		<pubDate>Tue, 11 Sep 2007 20:03:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-196</guid>
		<description>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.

Hans-Eric, I don't think it's even really a closure (or it wouldn't matter if it were not) if it doesn't use variables that have gone out of scope. And closure is incredibly useful for GUI code. E.g., (in browser JavaScript) half the time you use setTimeout. How many times have you seen someone assemble a string to pass as the first argument of setTimeout, potentially with escaping required and always making the later function query the DOM again to get back to the right element(s)? Passing a string invokes eval, which is asking for trouble, and the whole thing is a mess maintenance- and clarity-wise. Trivially solved with a real closure (as JavaScript can do, but many people don't realize).</description>
		<content:encoded><![CDATA[<p>Bookmarked, &#8217;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>Hans-Eric, I don&#8217;t think it&#8217;s even really a closure (or it wouldn&#8217;t matter if it were not) if it doesn&#8217;t use variables that have gone out of scope. And closure is incredibly useful for GUI code. E.g., (in browser JavaScript) half the time you use setTimeout. How many times have you seen someone assemble a string to pass as the first argument of setTimeout, potentially with escaping required and always making the later function query the DOM again to get back to the right element(s)? Passing a string invokes eval, which is asking for trouble, and the whole thing is a mess maintenance- and clarity-wise. Trivially solved with a real closure (as JavaScript can do, but many people don&#8217;t realize).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: renoX</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-195</link>
		<dc:creator>renoX</dc:creator>
		<pubDate>Tue, 11 Sep 2007 19:56:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-195</guid>
		<description>Well Foo as the author has shown, D has anonymous function so you can use them for GUIs, are closure really necessary for GUIs?</description>
		<content:encoded><![CDATA[<p>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>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans-Eric</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-193</link>
		<dc:creator>Hans-Eric</dc:creator>
		<pubDate>Tue, 11 Sep 2007 19:08:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-193</guid>
		<description>Foo: Oh but I do find closures *incredibly* useful. I use them all the time in Ruby and JavaScript (and I'd love to see the array trick you mention for Java). But I have yet to see a useful example which rely on variables that have lost their scope. (This is what I meant by "real closure" in the article). The examples I've seen, and the ones I have come up with myself are artificial and can often be solved with a nice object oriented approach. If you have a good example please show me. No one would be happier than me if you managed to prove me wrong.</description>
		<content:encoded><![CDATA[<p>Foo: Oh but I do find closures *incredibly* useful. I use them all the time in Ruby and JavaScript (and I&#8217;d love to see the array trick you mention for Java). But I have yet to see a useful example which rely on variables that have lost their scope. (This is what I meant by &#8220;real closure&#8221; in the article). The examples I&#8217;ve seen, and the ones I have come up with myself are artificial and can often be solved with a nice object oriented approach. If you have a good example please show me. No one would be happier than me if you managed to prove me wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Foo</title>
		<link>http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-192</link>
		<dc:creator>Foo</dc:creator>
		<pubDate>Tue, 11 Sep 2007 18:21:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.hans-eric.com/2007/09/11/d-doesnt-have-real-closures/#comment-192</guid>
		<description>Holy.  Crap.

I don't know what to be more astonished by: the fact that a *new* programming language doesn't have a basic, incredibly useful feature like closures (and it's *incredibly* useful for GUI work -- it lets me easily construct anonymous functions to add as hooks for things like button presses etc.) or the fact that in this day and age there's someone who actually thinks that closures aren't a big deal except for functional languages.

The languages where I use closures the *most* are:

    - Python
    - Java (using the array trick)
    - JavaScript

None of these languages is functional.  In all of them, closures are incredibly useful.  I can forgive ObjC for not having closures -- it's a language from the '80s -- but D?  No way in hell.</description>
		<content:encoded><![CDATA[<p>Holy.  Crap.</p>
<p>I don&#8217;t know what to be more astonished by: the fact that a *new* programming language doesn&#8217;t have a basic, incredibly useful feature like closures (and it&#8217;s *incredibly* useful for GUI work &#8212; it lets me easily construct anonymous functions to add as hooks for things like button presses etc.) or the fact that in this day and age there&#8217;s someone who actually thinks that closures aren&#8217;t a big deal except for functional languages.</p>
<p>The languages where I use closures the *most* are:</p>
<p>    - Python<br />
    - Java (using the array trick)<br />
    - JavaScript</p>
<p>None of these languages is functional.  In all of them, closures are incredibly useful.  I can forgive ObjC for not having closures &#8212; it&#8217;s a language from the &#8217;80s &#8212; but D?  No way in hell.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

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