Pseudo namespaces

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 3 13:29:51 PST 2015


On 12/03/2015 04:05 PM, FreeSlave wrote:
> I don't understand how it is namespace (it's just named scope for me)
> and why do we need template for that? Probably struct with static
> members would work the same without need for instantiating the template.

How would one use a struct for the List example? There's no access to 
"this". Yes, it's a named scope.

> When talking about namespaces in the C++ sense, the feature of namespace
> is that it can be scattered among many files and can be 'using'.

Wasn't thinking of C++ namespaces specifically, just an interesting 
artifact.

FWIW I wanted to use it to allow lst.linear.stable.insert() and 
lst.stable.linear.insert() to refer to the same function, but this is 
not working. Qualifies as a bug?

template _pseudo_namespace()
{
     void fun(int x)
     {
          import std.stdio;
          writeln(x);
     }
}
alias pseudo_namespace = _pseudo_namespace!();

struct List(T)
{
     template _stable()
     {
         void insert(int x)
         {
              import std.stdio;
              writeln(x);
         }
     }
     alias stable = _stable!();
     template _linear()
     {
         alias stable = _stable!();
     }
     alias linear = _linear!();
}

void main()
{
     pseudo_namespace.fun(1);
     List!int lst;
     lst._stable!().insert(2);
     lst.stable.insert(2);
     lst._linear!().stable.insert(2);
     lst.linear.stable.insert(2);
}


Andrei


More information about the Digitalmars-d mailing list