Implicit instantiation of parameterless templates

David Piepgrass qwertie256 at gmail.com
Fri Oct 5 09:06:23 PDT 2012


On Friday, 5 October 2012 at 12:24:12 UTC, Paulo Pinto wrote:
> On Friday, 5 October 2012 at 12:01:30 UTC, Piotr Szturmaj wrote:
>> Java and C# with their generics can do the following:
>>
>> class List { }
>> class List<T> { }
>>
>> List list = new List();
>> List<int> intList = new List<int>();
>>
>> In D similar code can't work because we can't have both a type 
>> and a template with the same name. So this code must be 
>> rewritten to:
...
>
> Why to you need this?
>
> Java and C# only allow this type of code due to backwards 
> compatibility, because their first version did not allow for 
> generics, and their creators did not want to force everyone to 
> recode their code bases.

The Java and C# situations are very different. In Java, generics 
are "erased" at runtime so a List<int> is the same thing as a 
List. In C#/.NET, however, List<int> and List are 'unrelated' 
types, which is what Piotr was talking about.

.NET allows "overloading" types based on the number of generic 
parameters, so for example Tuple<X> is a different type than 
Tuple<X,Y> (the runtime names are Tuple`1 and Tuple`2). Since C# 
has no "default arguments" for generics or "tuple template 
parameters", it is trivial to allow different types that have the 
same name but a different number of generic parameters. In D, 
however, the situation is a bit more complicated.


More information about the Digitalmars-d mailing list