Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloaded method-group argument confuses overload resolution?
    text
    copied!<p>The following call to the overloaded <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select.aspx" rel="noreferrer"><strong><code>Enumerable.Select</code></strong></a> method:</p> <pre><code>var itemOnlyOneTuples = "test".Select&lt;char, Tuple&lt;char&gt;&gt;(Tuple.Create); </code></pre> <p>fails with an ambiguity error (namespaces removed for clarity):</p> <pre><code>The call is ambiguous between the following methods or properties: 'Enumerable.Select&lt;char,Tuple&lt;char&gt;&gt; (IEnumerable&lt;char&gt;,Func&lt;char,Tuple&lt;char&gt;&gt;)' and 'Enumerable.Select&lt;char,Tuple&lt;char&gt;&gt; (IEnumerable&lt;char&gt;, Func&lt;char,int,Tuple&lt;char&gt;&gt;)' </code></pre> <p>I can certainly understand why <em>not</em> specifying the type-arguments explicitly would result in an ambiguity (both the overloads would apply), but I don't see one after doing so.</p> <p>It appears clear enough to me that the intention is to call the first overload, with the method-group argument resolving to <code>Tuple.Create&lt;char&gt;(char)</code>. The second overload should not apply because none of the <code>Tuple.Create</code> overloads can be converted to the expected <code>Func&lt;char,int,Tuple&lt;char&gt;&gt;</code> type. I'm <em>guessing</em> the compiler is confused by <code>Tuple.Create&lt;char, int&gt;(char, int)</code>, but its return-type is wrong: it returns a two-tuple, and is hence not convertible to the relevant <code>Func</code> type.</p> <p>By the way, any of the following makes the compiler happy:</p> <ol> <li>Specifying a type-argument for the method-group argument: <code>Tuple.Create&lt;char&gt;</code> (Perhaps this is actually a type-inference issue?).</li> <li>Making the argument a lambda-expression instead of a method-group: <code>x =&gt; Tuple.Create(x)</code>. (Plays well with type-inference on the <code>Select</code> call).</li> </ol> <p>Unsurprisingly, trying to call the other overload of <code>Select</code> in this manner also fails:</p> <pre><code>var itemIndexTwoTuples = "test".Select&lt;char, Tuple&lt;char, int&gt;&gt;(Tuple.Create); </code></pre> <p>What's the exact problem here?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload