Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>as</code> with an interface type uses an interface cast, which uses the GUID to find the interface. For a generic interface with a GUID, every instantiation gets the same GUID. If a single type implements multiple copies of the interface, then looking up by GUID will result in the first interface being returned.</p> <p>The program works as expected if you don't use an interface cast, and instead use an interface conversion like this:</p> <pre><code>procedure Test; var t : TTest&lt;integer, string&gt;; begin t := TTest&lt;integer, string&gt;.Create(39, 'Blah'); ShowMessage(ITest&lt;string&gt;(t).Val); ShowMessage(IntToStr(ITest&lt;Integer&gt;(t).Val)); end; </code></pre> <p>Originally, when generics were being implemented for Win32, GUIDs were not permitted on generic interfaces. However, dynamic querying for generic interfaces was desirable for generic container scenarios, and in general, as a mechanism for querying a service provider for type-specific services in the context of an algorithm (such as sorting or searching, which require things like comparers and equality tests). So a new plan was formed: have a GUID on the generic interface, but create a hash of the type arguments for generic instantiations and fold (e.g. xor) the hash into GUID to create a unique GUID for every distinct and incompatible instantiation. However, this was late in the day, and a good implementation was not possible within time constraints. But the requirement for dynamic querying remained, so the GUIDs stayed. That's why things are the way they are today.</p> <p>To solve your specific scenario, the best I can recommend is to use distinct descendants with explicit GUIDs; or use a different mechanism for querying for the interface.</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