<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx</link><description>Hey all, sorry for the long time between posts; I have been crazy busy recruiting , interviewing, fixing bugs, making performance improvements and implementing last-minute changes to the language and expression tree library. The last few posts about lambda</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1956959</link><pubDate>Tue, 27 Mar 2007 04:13:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1956959</guid><dc:creator>Name (required)</dc:creator><description>&lt;p&gt;Extension methods are another potential source for adding potential signatures.&lt;/p&gt;
&lt;p&gt;I assume that lambda expressions take extension methods into account when resolving signatures.&lt;/p&gt;
&lt;p&gt;For example, Add a ToUpper to In32 (whatever that may mean...) and M((int x)=&amp;gt;x.ToUpper()); should resolve fine.&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1959202</link><pubDate>Tue, 27 Mar 2007 07:56:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1959202</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Sure. &amp;nbsp;Almost all of the rules of semantic analysis are the same inside a lambda body as in a normal method. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1967556</link><pubDate>Wed, 28 Mar 2007 00:05:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1967556</guid><dc:creator>jmlovero</dc:creator><description>&lt;p&gt;If the compiler has a choice between an extension method and a native method when trying to resolve an overload, which one will it pick? &amp;nbsp;Or will it throw an exception? &amp;nbsp;Let's use the example that &amp;quot;Name (required)&amp;quot; provided and say that M((int x)=&amp;gt;x.ToUpper()); resolves because we've created a ToUpper() method that takes and returns an Integer. &amp;nbsp;What happens?&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1968223</link><pubDate>Wed, 28 Mar 2007 01:06:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1968223</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;If there are two or more applicable instance methods and we cannot determine which is better, we do NOT start looking for instance methods. We give an error as usual.&lt;/p&gt;
&lt;p&gt;If there are one or more applicable instance methods and we can determine which is best, we bind to the best instance method.&lt;/p&gt;
&lt;p&gt;Otherwise, there must be zero applicable instance methods. &amp;nbsp; We look for extension methods.&lt;/p&gt;
&lt;p&gt;The rules for how we look for extension methods are complicated and I can go into them in detail if you would like. &amp;nbsp;Suffice to say that we start looking in the current namespace for an extension method with the right name. &amp;nbsp;If we can't find one then we step out one level of namespace, etc.&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1968255</link><pubDate>Wed, 28 Mar 2007 01:13:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1968255</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;So in the example you mentioned, we would try both overloads for M. &amp;nbsp;If some strange person did an extension method on int to make x.ToUpper() legal on ints, then both overloads would be applicable candidates. &amp;nbsp;Since neither is better than the other, we would give an ambiguity error.&lt;/p&gt;
&lt;p&gt;In general, not just in the lambda binding case, introducing new possible bindings for any given name is a potentially breaking change. &amp;nbsp;It can change working code into code that has overload resolution ambiguities. &amp;nbsp;However the problem is _particularly_ pernicious in lambdas because of this ability to try multiple type bindings in a lambda and pick the one that works. &lt;/p&gt;
&lt;p&gt;We do NOT recommend introducing extension methods willy-nilly. &amp;nbsp;As the draft spec states, &amp;quot;Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible.&amp;quot;&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1978924</link><pubDate>Wed, 28 Mar 2007 21:06:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1978924</guid><dc:creator>jmlovero</dc:creator><description>&lt;p&gt;Thanks for the insight, Eric. &amp;nbsp;Your post was extremely informative as usual.&lt;/p&gt;
&lt;p&gt;I'm a bit surprised that you don't favor instance methods over extension methods as a way to disambiguate method calls. &amp;nbsp;I can understand the decision, but might it have been better to favor the instance method and give a compiler warning about the ambiguity? &amp;nbsp;This would avoid unforseen breaking changes due to the introduction of an extension method. &amp;nbsp;Was there a debate over this sort of thing on the compiler team? &amp;nbsp;Is it your opinion that any ambiguity is bad enough that the code should not compile? &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Lambda Expressions vs. Anonymous Methods, Part Four</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#1979255</link><pubDate>Wed, 28 Mar 2007 21:30:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1979255</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;We DO favour instance methods over extension methods when disambiguating method calls. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think I was insufficiently clear in my description above. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;What we need to do is determine whether the int or the string version of M (or both) is applicable. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Clearly the string version is a candidate.&lt;/p&gt;
&lt;p&gt;If there is an extension method on int such that the int version of the lambda can be successfully bound, then it is a candidate too.&lt;/p&gt;
&lt;p&gt;At this point we now need to decide which overload is better, and in this case neither is better. &amp;nbsp;We do not consider any of the reasons why a particular body bound successfully when deciding which is better. &amp;nbsp;We do not say &amp;quot;well, this body bound with twelve extension methods, but this other version of the body bound with only six extension methods, so the version with six is better&amp;quot;. &amp;nbsp;&amp;quot;Betterness&amp;quot; is determined solely by examining the types and the conversions, not any details of why the conversion succeeded.&lt;/p&gt;
&lt;p&gt;However, if we are in a situation where we have to choose between an instance method and an extension method, it is not even a choice. &amp;nbsp;If there is ANY applicable instance method, we either choose it or produce an ambiguity error. &amp;nbsp;We NEVER choose an applicable extension method over an applicable instance method.&lt;/p&gt;
</description></item><item><title>Lambda Expressions vs. Anonymous Methods, Part Five</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#4795782</link><pubDate>Fri, 07 Sep 2007 01:32:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4795782</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;Last time I demonstrated that the compiler could have to do an exponential number of bindings in order&lt;/p&gt;
</description></item><item><title>Learning WPF with BabySmash - Factories, Interfaces, Delegates and Lambdas, oh my!</title><link>http://blogs.msdn.com/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx#8601211</link><pubDate>Sun, 15 Jun 2008 18:31:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8601211</guid><dc:creator>Readed By Wrocław NUG members</dc:creator><description>&lt;p&gt;NOTE: If you haven&amp;amp;#39;t read the first post in this series, I would encourage you do to that first&lt;/p&gt;
</description></item></channel></rss>