Specifying C++ symbols in C++ namespaces

monarch_dodra monarchdodra at gmail.com
Sun Apr 6 14:00:34 PDT 2014


On Sunday, 6 April 2014 at 20:17:09 UTC, Walter Bright wrote:
> D has closed scopes, and members can be "added" to external 
> classes just fine, using CTFE.

You mean UFCS? As in, for example, "front" for arrays? It doesn't 
work as well as advertised though. The issue is that the added 
members are only visible if the *called* code knows to import the 
*caller* code. This breaks as soon as you leave your own internal 
ecosystem.

//----
import a;

struct S //Some object
{
}

//Extend to make it a range.
bool empty(S);
int front(S);
void popFront(S);

//Try it.
void main()
{
     S s;
     foreach(e; s) //Error: invalid foreach aggregate s
         writeln(s);
     s.array(); //Error: template std.range.take cannot...
     a.foo(s); //Error: no property 'popFront' for type 'S'
}
//----
module a;

void foo(T)(T t)
{
     t.popFront();
}
//----
Well that horribly fails.

Because when calling a template, you are only importing the 
passed argument, but not his ecosystem with it. The only way in D 
to "really" extend an existing object, is to wrap it into a new 
object. And even then, the wrapping is limited, because you can't 
orthogonally wrap (eg the wrapping linearly stacks, whereas UCFS 
*could* add two completely independent functionalities). Not to 
mention issues with code bloat...

C++'s namespaces have their flaws, but they *are* scalable in a 
way D's modules aren't, thanks to Koenig Lookup.


More information about the Digitalmars-d mailing list