[typing] Type-erasure re generics

Steven Schveighoffer schveiguy at yahoo.com
Wed Sep 29 11:54:30 PDT 2010


On Wed, 29 Sep 2010 14:39:43 -0400, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On Wednesday, September 29, 2010 11:11:53 Steven Schveighoffer wrote:
>> I think actually that concept is in the latest C# with type
>> contra-variance.
>>
>> Essentially, you should be able to implicitly cast Container!(B) to
>> const(Container!(A)) conceptually, but actually you may have to require
>> only pure calls (I don't think there's a way to do this).
>>
>> I think C# deals with this via designating certain types on the template
>> parameters as "input" or "output" types, and restricting functions from
>> using types in the wrong direction.
>
> There may be a way to get it to work with const or some other sort of
> constraints that disallowed mutation - or at least mutating what is  
> directly in
> the container (mutating which is referred to by the elements in the  
> container
> could work) - but you can't allow mutation of the elements directly in  
> the
> container or you'd be allowed to put incorrect types in the container.  
> I'd hate
> to be the one to try to get such a feature to work without const like  
> they would
> have had to do in C#.

I think it may have to be more than just const:

class C(T)
{
    static T globalval;
}

class A {}
class B : A {}

void foo(C!B c)
{
   const(C!A) c2 = c;
   c2.globalval = new A;
}

If the entire class is marked as pure (with pure defined as weak purity a  
la Don's idea) that might work as setting globalval would be illegal.

-Steve


More information about the Digitalmars-d mailing list