Note that there are some explanatory texts on larger screens.

plurals
  1. PODo Generics Mess Up Interface Name Mapping?
    primarykey
    data
    text
    <p>Basically, I want a class to be able to implement two different versions of the same generic interface. </p> <p>Consider this code </p> <pre><code>type // a generic interface ITest&lt;T&gt; = interface ['{6901FE04-8FCC-4181-9E92-85B73264B5DA}'] function Val: T; end; // a class that purports to implement two different types of that interface TTest&lt;T1, T2&gt; = class(TInterfacedObject, ITest&lt;T1&gt;, ITest&lt;T2&gt;) protected fV1: T1; fV2: T2; public constructor Create(aV1: T1; aV2: T2); function Val: T1; // Val() for ITest&lt;T1&gt; function T2Val: T2; // Val() for ITest&lt;T2&gt; function ITest&lt;T2&gt;.Val = T2Val; // mapping end; constructor TTest&lt;T1, T2&gt;.Create(aV1: T1; aV2: T2); begin inherited Create; fV1 := aV1; fV2 := aV2; end; function TTest&lt;T1, T2&gt;.T2Val: T2; begin result := fV2; end; function TTest&lt;T1, T2&gt;.Val: T1; begin result := fV1; end; ///////////// procedure Test; var t : TTest&lt;integer, string&gt;; begin t := TTest&lt;integer, string&gt;.Create(39, 'Blah'); ShowMessage((t as ITest&lt;string&gt;).Val); // this works as expected ShowMessage(IntToStr((t as ITest&lt;integer&gt;).Val)); // this gets AV end; </code></pre> <p>The first ShowMessage displays 'Blah' as I would expect, but the second crashes. The reason it crashes is because the call invokes T2Val() instead of Val() as I would have expected. Apparently the conflict resolution mapping maps the method for both types of interfaces and not just for ITest: T2.</p> <p>So, here's my question(s). </p> <p>Is this a bug? By which, I mean, did Embarcadero intend for this to be supported and simply implement it wrong? Or did they never have any intention of allowing programmers to do something like this at all? (Honestly, I was a little surprised that my test program even compiled)</p> <p>If this is a bug, does anyone have any idea if there might be a workaround to let me have one class support two different types of a single generic interface?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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