Templates convariance

michal.minich at gmail.com michal.minich at gmail.com
Wed Jun 21 03:24:31 PDT 2006


Maybe this is the way how to solve this
http://lambda-the-ultimate.org/node/1573

..and a publication linked from this blog entry
http://research.microsoft.com/research/pubs/view.aspx?type=inproceedings&id=1215



In article <e7b3q2$22m5$1 at digitaldaemon.com>, Bruno Medeiros says...
>
>Sean Fritz wrote:
>> In article <e799ce$2idm$1 at digitaldaemon.com>, michal.minich at gmail.com says...
>> 
>> A list!(Shape) is not the same a a list!(Square).  It sounds like you want some
>> sort of wildcard matching or something to enforce type bounding like Java/C#
>> have, but casts are definitely unsafe.
>> 
>> Example code (I'm going to blotch various syntax, sorry in advance):
>> 
>> void foo(List!(Shape) ls) {
>> ls.add(new Circle) //passes type check locally?
>> }
>> 
>> ...
>> List!(Square) ls = new List!(Square);
>> foo(ls);
>> //but ls now contains a Circle?  Circles don't go in List!(Square)?
>> ,,.
>> 
>> 
>
>Indeed. I was about to mention that too, because this is a more general 
>problem concerning parameterized collections (Lists, arrays, etc.) in 
>that a parameterized collection type is only covariant with another 
>parameterized collection type if the parameter is invariant. (because 
>the parameter is both an input and an ouput)
>So for example an array of Numbers can not be (safely) typed as an array 
>of Integers, nor it cannot be typed as an array of Objects.
>Still, an array of Numbers can be seen as a readonly array of Objects, 
>and as a writeonly array of Integers.
>
>-- 
>Bruno Medeiros - CS/E student
>http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D





More information about the Digitalmars-d mailing list