template+alias as an object's "namespace"

Timon Gehr timon.gehr at gmx.ch
Wed Apr 18 23:44:11 PDT 2012


On 04/19/2012 01:41 AM, F i L wrote:
> kennytm wrote:
>> struct Foo1 {
>>     struct Unsafe {
>>     static:
>>         void bar() { ... }
>>     }
>> }
>> Foo1 f;
>> f.Unsafe.bar();
>
> That fails when you add variables though:
>
>    struct Foo {
>      struct Unsafe {
>        string name;
>        static void bar() {
>          writeln(name); // error: needs 'this'
>        }
>      }
>    }
>

struct Foo {
   struct Unsafe {
   static:
     string name;
     void bar() {
       writeln(name); // fine
     }
   }
}


> Like I said, it's completely possible with templates today, you just
> have to alias them for nice syntax. Parameterless templates

It is not really a 'template' if it is parameterless and does not need 
to be instantiated.

> would just
> be sugar, but that sugar would be nice when you want to use
> classifications like this in real code.

This idiom imho shouldn't be overused. Renamed imports usually suffice.


More information about the Digitalmars-d mailing list