Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem understanding C# type inference as described in the language specification
    text
    copied!<p>The <strong><a href="http://www.microsoft.com/downloads/en/details.aspx?familyid=DFBF523C-F98C-4804-AFBD-459E846B268E&amp;displaylang=en" rel="noreferrer">C# language specification</a></strong> describes type inference in Section §7.5.2. There is a detail in it that I don’t understand. Consider the following case:</p> <pre><code>// declaration void Method&lt;T&gt;(T obj, Func&lt;string, T&gt; func); // call Method("obj", s =&gt; (object) s); </code></pre> <p>Both the Microsoft and Mono C# compilers correctly infer <code>T</code> = <code>object</code>, but my understanding of the algorithm in the specification would yield <code>T</code> = <code>string</code> and then fail. Here is how I understand it:</p> <p><strong>The first phase</strong></p> <ul> <li><p>If Ei is an anonymous function, an <em>explicit parameter type inference</em> (§7.5.2.7) is made from Ei to Ti</p> <p>⇒ has no effect, because the lambda expression has no explicit parameter types. Right?</p></li> <li><p>Otherwise, if Ei has a type U and xi is a value parameter then a <em>lower-bound inference</em> is made from U to Ti.</p> <p>⇒ the first parameter is of static type <code>string</code>, so this adds <code>string</code> to the lower bounds for <code>T</code>, right?</p></li> </ul> <p><strong>The second phase</strong></p> <ul> <li><p>All <em>unfixed</em> type variables Xi which do not <em>depend on</em> (§7.5.2.5) any Xj are fixed (§7.5.2.10).</p> <p>⇒ <code>T</code> is unfixed; <code>T</code> doesn’t depend on anything... so <code>T</code> should be fixed, right?</p></li> </ul> <p><strong>§7.5.2.11 Fixing</strong></p> <ul> <li><p>The set of candidate types Uj starts out as the set of all types in the set of bounds for Xi.</p> <p>⇒ { <code>string</code> (lower bound) }</p></li> <li><p>We then examine each bound for Xi in turn: [...] For each lower bound U of Xi all types Uj to which there is not an implicit conversion from U are removed from the candidate set. [...]</p> <p>⇒ doesn’t remove anything from the candidate set, right?</p></li> <li><p>If among the remaining candidate types Uj there is a unique type V from which there is an implicit conversion to all the other candidate types, then Xi is fixed to V.</p> <p>⇒ Since there is only one candidate type, this is vacuously true, so Xi is fixed to <code>string</code>. Right?</p></li> </ul> <hr> <p><strong>So where am I going wrong?</strong></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