<?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: Anonymous Methods &#8211; When Should They Be Used?</title>
	<atom:link href="http://www.deltics.co.nz/blog/Index.php?feed=rss2&#038;p=48" rel="self" type="application/rss+xml" />
	<link>http://www.deltics.co.nz/blog/?p=48</link>
	<description>Keeping Delphi afloat in Aotearoa</description>
	<lastBuildDate>Fri, 03 Sep 2010 13:44:08 +1200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Caleb</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-16</link>
		<dc:creator>Caleb</dc:creator>
		<pubDate>Wed, 06 Aug 2008 13:51:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-16</guid>
		<description>(disclaimer: I have a fair bit of experience in both Delphi and Python)

You mostly get the idea, and I like the demonstration comparing storing state in a class vs. in a closure (&quot;anonymous method&quot; is such a dumb name - I wonder whether this was introduced because it got too difficult to distinguish between .NET 1, .NET 2 and .NET 3 versions of delegates?), and that is a very good way to understand the mechanism, i.e. an alternative method of binding state and action.  However, some points:

1. It will be easier, in general, to manage concurrency with closures, than for storing state via class fields, because the &quot;external references&quot; will have been &quot;captured&quot; by the closure, i.e. they can&#039;t change inside the closure.  This is not a Delphi-specific thing, and I don&#039;t know if there are plans to put such guarantees or checks in the compiler, or anything like that, but this is how functional programming languages approach concurrency - closures with no side effects.  Within methods (object methods), side effects from inside the method scope are not only easy, but even normal (this is the OOP paradigm).    You&#039;re very close to seeing the benefit of closures for concurrency with your

mm := TMotile.Execute( procedure; begin   end; );  

example; now just try to see that the closure you just defined can be *intrinsically* thread-safe, because all the state it needs has been bound and made available at the perfect place :).   (I don&#039;t know how side-effects are managed in Tiburon, just giving a brief description of the closure-concurrency relationship as it might apply to Delphi)

2. In addition to concurrency, closures are very often used for event programming.  For instance, a commonly used example is when, say, you want to remember something during a mouse-down, and recall it during mouse-up (e.g. drag-n-drop).  With classes, you would store the data in a field and recall it later, whereas with closures, you write the mouse-up event handler inside the mouse-down event (capture context-specific state too), and assign the method pointer to the mouse-up event slot.   Again, two different approaches.   Technical merits of each aside, it is informative to think about different ways of doing things.  My personal opinion is that this is a far more intuitive and logical means of writing connected events than storing temporary data inside an object instance, but ymmv.

3.  I disagree that with closures, anything has been &quot;hidden&quot;, or maybe I fail to understand what you mean (the method name?).  I also disagree that closures are &quot;simpler&quot; in some sense.   They are simply another method of binding state and action, that&#039;s all.  

4. The &quot;with&quot; strawman is not convincing.  The problem with &quot;with&quot; is the possibility (indeed, likelihood, in large projects!) of namespace collisions, not the fact that information is hidden, per se.  There is no similarity with closures.  Again, the fact that the closures do not have names, or that they are called &quot;anonymous methods&quot; in *this* implementation has nothing to do with the functionality -&gt; which is the binding of state and function.  &quot;Anonymous methods&quot; is a really dumb name.   Almost as dumb as &quot;Lambda expressions&quot;, but at least there is a hint there that something else is going.

5. I disagree that closures should only be used when one *has* to.  I would rather promote that closures should be assignable (and used) throughout the Delphi event subsystem.  As far as I know, according to Andreano Lanusse and Barry Kelly, this is not (yet) possible.   Using closures as events would clean up many class declarations of fields that are simply there to propagate state from one event handler to another.

6.  You neglected to mention that Delphi already has a type of &quot;closure&quot;---not the function pointers of your &quot;Cook&quot; example above, but rather nested functions and procedures.  However, they are dangerous because they do not *bind* the enclosing scope, i.e. A nested function that refers to local variables in the parent function will continue to use the newly updated values of those local variables throughout the execution path of the parent function.   This would be where angels fear to tread.

7.  Everyone knows generics and closures are coming to Delphi Win32 because they are required in Delphi.NET which is required to support C#/.NET dynamic features.  This is obvious, and a given.  That said, some of those features, like closures, might prove worthwhile in spite of their origins.  I am less enthusiastic about generics, which, imho is just a typing-saver and is likely to make debugging more difficult.   Closures will also make debugging more difficult (where do you set the breakpoint???) but the advantages are a little clearer.  At least for me.  

On the whole, a good post, and clearly written.   I note that you have not outright said &quot;anonymous methods are bad!&quot;, but have rather chosen to require evidence, and this is a good thing.

(I&#039;m probably not going to swing by here and read replies to this comment, unless I get a DelphiFeeds update...)

Cheers
Caleb</description>
		<content:encoded><![CDATA[<p>(disclaimer: I have a fair bit of experience in both Delphi and Python)</p>
<p>You mostly get the idea, and I like the demonstration comparing storing state in a class vs. in a closure (&#8221;anonymous method&#8221; is such a dumb name &#8211; I wonder whether this was introduced because it got too difficult to distinguish between .NET 1, .NET 2 and .NET 3 versions of delegates?), and that is a very good way to understand the mechanism, i.e. an alternative method of binding state and action.  However, some points:</p>
<p>1. It will be easier, in general, to manage concurrency with closures, than for storing state via class fields, because the &#8220;external references&#8221; will have been &#8220;captured&#8221; by the closure, i.e. they can&#8217;t change inside the closure.  This is not a Delphi-specific thing, and I don&#8217;t know if there are plans to put such guarantees or checks in the compiler, or anything like that, but this is how functional programming languages approach concurrency &#8211; closures with no side effects.  Within methods (object methods), side effects from inside the method scope are not only easy, but even normal (this is the OOP paradigm).    You&#8217;re very close to seeing the benefit of closures for concurrency with your</p>
<p>mm := TMotile.Execute( procedure; begin   end; );  </p>
<p>example; now just try to see that the closure you just defined can be *intrinsically* thread-safe, because all the state it needs has been bound and made available at the perfect place <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .   (I don&#8217;t know how side-effects are managed in Tiburon, just giving a brief description of the closure-concurrency relationship as it might apply to Delphi)</p>
<p>2. In addition to concurrency, closures are very often used for event programming.  For instance, a commonly used example is when, say, you want to remember something during a mouse-down, and recall it during mouse-up (e.g. drag-n-drop).  With classes, you would store the data in a field and recall it later, whereas with closures, you write the mouse-up event handler inside the mouse-down event (capture context-specific state too), and assign the method pointer to the mouse-up event slot.   Again, two different approaches.   Technical merits of each aside, it is informative to think about different ways of doing things.  My personal opinion is that this is a far more intuitive and logical means of writing connected events than storing temporary data inside an object instance, but ymmv.</p>
<p>3.  I disagree that with closures, anything has been &#8220;hidden&#8221;, or maybe I fail to understand what you mean (the method name?).  I also disagree that closures are &#8220;simpler&#8221; in some sense.   They are simply another method of binding state and action, that&#8217;s all.  </p>
<p>4. The &#8220;with&#8221; strawman is not convincing.  The problem with &#8220;with&#8221; is the possibility (indeed, likelihood, in large projects!) of namespace collisions, not the fact that information is hidden, per se.  There is no similarity with closures.  Again, the fact that the closures do not have names, or that they are called &#8220;anonymous methods&#8221; in *this* implementation has nothing to do with the functionality -&gt; which is the binding of state and function.  &#8220;Anonymous methods&#8221; is a really dumb name.   Almost as dumb as &#8220;Lambda expressions&#8221;, but at least there is a hint there that something else is going.</p>
<p>5. I disagree that closures should only be used when one *has* to.  I would rather promote that closures should be assignable (and used) throughout the Delphi event subsystem.  As far as I know, according to Andreano Lanusse and Barry Kelly, this is not (yet) possible.   Using closures as events would clean up many class declarations of fields that are simply there to propagate state from one event handler to another.</p>
<p>6.  You neglected to mention that Delphi already has a type of &#8220;closure&#8221;&#8212;not the function pointers of your &#8220;Cook&#8221; example above, but rather nested functions and procedures.  However, they are dangerous because they do not *bind* the enclosing scope, i.e. A nested function that refers to local variables in the parent function will continue to use the newly updated values of those local variables throughout the execution path of the parent function.   This would be where angels fear to tread.</p>
<p>7.  Everyone knows generics and closures are coming to Delphi Win32 because they are required in Delphi.NET which is required to support C#/.NET dynamic features.  This is obvious, and a given.  That said, some of those features, like closures, might prove worthwhile in spite of their origins.  I am less enthusiastic about generics, which, imho is just a typing-saver and is likely to make debugging more difficult.   Closures will also make debugging more difficult (where do you set the breakpoint???) but the advantages are a little clearer.  At least for me.  </p>
<p>On the whole, a good post, and clearly written.   I note that you have not outright said &#8220;anonymous methods are bad!&#8221;, but have rather chosen to require evidence, and this is a good thing.</p>
<p>(I&#8217;m probably not going to swing by here and read replies to this comment, unless I get a DelphiFeeds update&#8230;)</p>
<p>Cheers<br />
Caleb</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maël Hörz</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-12</link>
		<dc:creator>Maël Hörz</dc:creator>
		<pubDate>Tue, 05 Aug 2008 21:17:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-12</guid>
		<description>Well written! I mostly had the same thoughts when reading the &quot;Joel on Software&quot; post.

So far the only use I can see for anonymous methods is when they are really succinct. For example a sort function that requires a comparison method as argument.</description>
		<content:encoded><![CDATA[<p>Well written! I mostly had the same thoughts when reading the &#8220;Joel on Software&#8221; post.</p>
<p>So far the only use I can see for anonymous methods is when they are really succinct. For example a sort function that requires a comparison method as argument.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jolyon Smith</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-9</link>
		<dc:creator>Jolyon Smith</dc:creator>
		<pubDate>Tue, 05 Aug 2008 09:38:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-9</guid>
		<description>Thanks Lars.  I see what you mean about the anonymity being apparent (or not, if you see what I mean :) ) on the framework side, but to that extent anonymous methods are no more anonymous than the current ability to pass a method reference.

Surely the anonymous method name stems from the fact that the method reference being passed by reference will no longer need to have an identity, i.e. the anonymity apparent (again, or not) from the consumer side of the framework?


Re your PS - I have no doubt that my name isn&#039;t well known in non-tech, although I&#039;m not so sure whether that would count in my favour or against me!  ;)

This blog is, in part at least, an attempt to rehabilitate myself in the community.

:)</description>
		<content:encoded><![CDATA[<p>Thanks Lars.  I see what you mean about the anonymity being apparent (or not, if you see what I mean <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) on the framework side, but to that extent anonymous methods are no more anonymous than the current ability to pass a method reference.</p>
<p>Surely the anonymous method name stems from the fact that the method reference being passed by reference will no longer need to have an identity, i.e. the anonymity apparent (again, or not) from the consumer side of the framework?</p>
<p>Re your PS &#8211; I have no doubt that my name isn&#8217;t well known in non-tech, although I&#8217;m not so sure whether that would count in my favour or against me!  <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>This blog is, in part at least, an attempt to rehabilitate myself in the community.</p>
<p> <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars Fosdal</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-8</link>
		<dc:creator>Lars Fosdal</dc:creator>
		<pubDate>Tue, 05 Aug 2008 09:13:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-8</guid>
		<description>Good article.  As you know, I see the anon.methods in a slightly different light.  The anonymity is only apparent from the framework side, ie the plumbing that you pass your code into, while in your code - it is at the center of attention.  It is defined and passed to the framework at the location where it is being used.  

You ask why we use links in web pages, instead of quoting?  When we describe code - we actually do quote for clarity.  Without anon.methods, we are forced to &quot;link&quot; to our code (ie refer to some other function), instead of &quot;quoting&quot; what we want to do at the place we want to do it.

P.S. Your name is well known in delphi.non-tech, and I am sure that Dennis Gurock would not have any issues with adding you to the Delphifeeds list.  It never hurts to ask :)</description>
		<content:encoded><![CDATA[<p>Good article.  As you know, I see the anon.methods in a slightly different light.  The anonymity is only apparent from the framework side, ie the plumbing that you pass your code into, while in your code &#8211; it is at the center of attention.  It is defined and passed to the framework at the location where it is being used.  </p>
<p>You ask why we use links in web pages, instead of quoting?  When we describe code &#8211; we actually do quote for clarity.  Without anon.methods, we are forced to &#8220;link&#8221; to our code (ie refer to some other function), instead of &#8220;quoting&#8221; what we want to do at the place we want to do it.</p>
<p>P.S. Your name is well known in delphi.non-tech, and I am sure that Dennis Gurock would not have any issues with adding you to the Delphifeeds list.  It never hurts to ask <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jolyon Smith</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-6</link>
		<dc:creator>Jolyon Smith</dc:creator>
		<pubDate>Mon, 04 Aug 2008 21:18:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-6</guid>
		<description>Hello &quot;hello&quot;.. thanks for the comment.

I am supposing that you are offering Ruby as an example where anonymous methods already exist and provide the &quot;real&quot; examples I asked for.

A fair point in it&#039;s way, but if was writing the sort of code that Ruby is useful for, or if Ruby was well suited to the sorts of application I do write, I would already be using Ruby.

So I guess what I am really asking for is &quot;examples that I can see apply to the way I use Delphi and the sort of code I work with&quot;.</description>
		<content:encoded><![CDATA[<p>Hello &#8220;hello&#8221;.. thanks for the comment.</p>
<p>I am supposing that you are offering Ruby as an example where anonymous methods already exist and provide the &#8220;real&#8221; examples I asked for.</p>
<p>A fair point in it&#8217;s way, but if was writing the sort of code that Ruby is useful for, or if Ruby was well suited to the sorts of application I do write, I would already be using Ruby.</p>
<p>So I guess what I am really asking for is &#8220;examples that I can see apply to the way I use Delphi and the sort of code I work with&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jolyon Smith</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-5</link>
		<dc:creator>Jolyon Smith</dc:creator>
		<pubDate>Mon, 04 Aug 2008 21:10:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-5</guid>
		<description>Thanks for the comments CR.

I hope to get onto DelphiFeeds at some point, but usually a blog has to have been running for a while (3-6 months) to qualify.  I may approach them in a few weeks though to see if my content up to that point is considered good enough to warrant an exemption.

In the meantime, tell your friends.

:)</description>
		<content:encoded><![CDATA[<p>Thanks for the comments CR.</p>
<p>I hope to get onto DelphiFeeds at some point, but usually a blog has to have been running for a while (3-6 months) to qualify.  I may approach them in a few weeks though to see if my content up to that point is considered good enough to warrant an exemption.</p>
<p>In the meantime, tell your friends.</p>
<p> <img src='http://www.deltics.co.nz/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hello</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-4</link>
		<dc:creator>hello</dc:creator>
		<pubDate>Mon, 04 Aug 2008 18:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-4</guid>
		<description>In Ruby, blocks are closures, i.e. what is called anonymous methods in Delphi. It can be used in iterator methods which yield values from the block.</description>
		<content:encoded><![CDATA[<p>In Ruby, blocks are closures, i.e. what is called anonymous methods in Delphi. It can be used in iterator methods which yield values from the block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CR</title>
		<link>http://www.deltics.co.nz/blog/?p=48&#038;cpage=1#comment-3</link>
		<dc:creator>CR</dc:creator>
		<pubDate>Mon, 04 Aug 2008 12:14:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.deltics.co.nz/blog/?p=48#comment-3</guid>
		<description>Good post.  At the current time, the only thing that seems a clear benefit with anonymous methods being brought in (and even then only a minor one) is the side effect of a new procedure type agonistic between global routines and methods.  That said, I look forward to seeing answers to your closing question putting me right though!

PS - you should get your blog aggregated by the Delphi Feeds site to allow more people to realise it exists.</description>
		<content:encoded><![CDATA[<p>Good post.  At the current time, the only thing that seems a clear benefit with anonymous methods being brought in (and even then only a minor one) is the side effect of a new procedure type agonistic between global routines and methods.  That said, I look forward to seeing answers to your closing question putting me right though!</p>
<p>PS &#8211; you should get your blog aggregated by the Delphi Feeds site to allow more people to realise it exists.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
