template+alias as an object's "namespace"

F i L witte2008 at gmail.com
Thu Apr 19 12:45:21 PDT 2012


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

Foo.Unsafe.name is static and not per-instance. That's a 
completely different thing than having a "namespace" within the 
object which serves only as a semantic layer to classify property 
access.


>> 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.

Sure it is. Templates provide a semantic convenience over 
reusable bits of code that get resolved at CT. It's more like a 
CT struct than a CT function. So it makes perfect sense to have 
parameterless ones, especially because you can already have that, 
only with less pretty code.


>> 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.

I don't see how import renaming applies to what I've suggested. 
It's a completely different topic. I'm sure it could be abused, 
like anything else, but It is useful in some areas, and I don't 
see any reason not to have a usable syntax for something that's 
already achievable in D.

Honestly I think this could be another "nah nah nah, look what 
our language can do" bullet point in D's favor. So far as I know, 
no other (efficient) language can sub-classify it's objects 
without increasing their per-instance memory footprint + require 
manually wiring up the connection. While in D it's simply an 
emergent feature of a beautiful template design.

D gives more control over the structure of objects than any other 
language, without sacrificing a thing. That sounds like a good 
marketing point to me :)


More information about the Digitalmars-d mailing list