Implicit instantiation of parameterless templates

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Oct 5 12:30:13 PDT 2012


On 05-Oct-12 23:25, F i L 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:
>>
>> class List(T = Variant) { }
>>
>> List!() list = new List!();
>> List!int intList = new List!int;
>>
>> When template name is used as a type and it can be instantiated with
>> no parameters it could be automatically rewritten to List!() by the
>> compiler. That code would then look like this:
>>
>> List list = new List;
>> List!int intList = new List!int;
>>
>> The question is... is it possible to change D's behaviour to avoid
>> awkward !() template parameters _without_ breaking backward
>> compatibility?
>
> +1 This is natural.
>
> Plus, this has other uses I've mentioned on here in the past,
> "sub-scoping":
>
>      class Foo
>      {
>          int bar;
>
>          template baz
>          {
>              int bar;
>          }
>      }
>
>      void main()
>      {
>          auto f = new Foo();
>
>          f.bar = 0;
>          f.baz.bar = 1;
>      }
>
> Currently, this syntax is possible, but requires to some ugly work-arounds.

Hm... If placed at the global scope it looks like a namespace.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list