Is there any good reason why C++ namespaces are "closed" in D?

Steven Schveighoffer schveiguy at gmail.com
Sat Aug 4 12:10:54 UTC 2018


On 8/3/18 5:20 PM, Walter Bright wrote:

> Telling them their code is 
> **** and that they should rewrite it in order to work with D is never, 
> ever going to work.

And that's OK with me. I'm OK with D saying it only supports reasonably 
written C++ code, and 99% of the C++ community would agree. If that 
means we lose one "customer" who doesn't use sane namespace practices, 
then I guess they can stick with C++.

>> are there any other additional benefits to the current design which 
>> I'm overlooking?
>>
>> With a non-scoped extern(c++) we could simply use two files.
> 
> Yes. But then you'll have the people who want a 1:1 correspondence with 
> their C++ files and the corresponding D files. I happen to be one of 
> those people, for the ease of maintaining a translation (and for 
> comparing it with the original C++ source code if it is not behaving 
> correctly).

I have to take this to mean that you are someone who wants 1:1 
correspondence, and not someone who puts 2 namespaces in the same file 
with identical symbol names.

In either case, I think it's forgivable for D to not be able to 
completely mimic C++ in every possible manner.

> Besides, I provided solutions for both Manu's case and Atila's (they are 
> different), which are easier than "simply" breaking things up into 
> multiple files.

But the solution is crazy backwards. You should be able to put in weird 
aliases and namespaces (or even split into multiple modules) if you have 
really crappy C++ code with namespace issues. Aliases and bizarre mixins 
shouldn't be the default *just in case* you have crappy code.

Note, I can do this to make namespaces if I want to mimic crappy C++ 
code in the same file (assuming extern(C++, ns) just affects mangling):

template _ns()
{
     extern(C++, ns) void foo(int) {}
}
alias ns = _ns!();
alias foo = ns.foo; // optional
template _ns2()
{
     extern(C++, ns2) void foo(int) {}
}
alias ns2 = _ns2!();
alias foo = ns2.foo; // optional

void main()
{
     // foo(1); // ERROR!
     ns.foo(1); // OK
     ns2.foo(1); // OK
}

Why can't we just tell C++ devs who have this horrible situation to do 
it this way, and leave all the reasonably named namespaces to use the 
awesome D module system as expected?

-Steve


More information about the Digitalmars-d mailing list