Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, let’s examine the first one. The error is:</p> <blockquote> <p>The type 'V' cannot be used as type parameter 'V' in the generic type or method 'Test.Generic_Route'. There is no implicit reference conversion from 'V' to 'Test.Generic_Visit'.</p> </blockquote> <p>It complains about this declaration:</p> <pre><code>public abstract class Generic_TSPRoute&lt;V, E&gt; : Generic_Route&lt;V, E&gt; where V : Concrete_TSPVisit where E : Concrete_TSPNode </code></pre> <p>This establishes two definitions:</p> <ul> <li><p><code>V</code> is a <code>Concrete_TSPVisit</code> (or a descendent of it)</p></li> <li><p><code>E</code> is a <code>Concrete_TSPNode</code> (or a descendent of it)</p></li> </ul> <p>Now let’s see what <code>Generic_Route&lt;V, E&gt;</code> lets us put in:</p> <pre><code>public class Generic_Route&lt;V, E&gt; where V : Generic_Visit&lt;E&gt; where E : GenericElement </code></pre> <p>The second constraint is fine because <code>Concrete_TSPNode</code> is a <code>GenericElement</code>. The first one is problematic: Remember that <code>E</code> is a <code>Concrete_TSPNode</code> <em>or a descendent of it</em>, therefore <code>Generic_Visit&lt;E&gt;</code> could be:</p> <ul> <li><p><code>Generic_Visit&lt;Concrete_TSPNode&gt;</code>, <strong>or</strong></p></li> <li><p><code>Generic_Visit&lt;some subclass of Concrete_TSPNode&gt;</code></p></li> </ul> <p>However, we also know from earlier that <code>V</code> is a <code>Concrete_TSPVisit</code> (or a descendent of it).</p> <ul> <li><p><code>Concrete_TSPVisit</code> inherits from <code>Generic_TSPVisit&lt;Concrete_TSPNode&gt;</code></p></li> <li><p><code>Generic_TSPVisit&lt;Concrete_TSPNode&gt;</code> inherits from <code>Generic_Visit&lt;Concrete_TSPNode&gt;</code></p></li> </ul> <p>Notice something? This requires it to be a <code>Generic_Visit&lt;Concrete_TSPNode&gt;</code>. It is emphatically not allowed to be <code>Generic_Visit&lt;some subclass of Concrete_TSPNode&gt;</code>.</p> <p><strong>In other words,</strong> imagine I write this:</p> <pre><code>var route = new Generic_TSPRoute&lt;Concrete_TSPVisit, Concrete_TSPNode_Subclass&gt;(); </code></pre> <p>According to your hierarchy, <code>Concrete_TSPVisit</code> is a <code>Generic_Visit&lt;Concrete_TSPNode&gt;</code> and therefore has a property that looks like</p> <pre><code>public Concrete_TSPNode Element { get; set; } </code></pre> <p>If I retrieve a value from this property, it is only guaranteed to be a <code>Concrete_TSPNode</code> but not necessarily a <code>Concrete_TSPNode_Subclass</code>.</p> <h2>EDIT:</h2> <p>I’ll leave this answer because it explains the reason for the compiler error, but Enigmativity’s answer actually provides the solution to the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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