DIP61: Add namespaces to D

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 26 05:37:01 PDT 2014


On 04/26/2014 11:31 AM, Walter Bright wrote:
> http://wiki.dlang.org/DIP61
>
> Best practices in C++ code increasingly means putting functions and
> declarations in namespaces. Currently, there is no support in D to call
> C++ functions in namespaces. The primary issue is that the name mangling
> doesn't match. Need a simple and straightforward method of indicating
> namespaces.
>
> There have been many proposals earlier:
>
>    http://forum.dlang.org/post/lhi1lt$269h$1@digitalmars.com
>
> but it seems to me that the simplest, most straightforward approach
> would be better.
> ...

I agree.

> As more and more people are attempting to call C++ libraries from D,
> this is getting to be a more and more important issue.

Looks good to me, but I think that the current limited lookup rules for 
template mixins are not really good enough to accommodate for common 
usage patterns of namespaces.

I think the following should both just work:

import std.stdio;

mixin template Foo(T){
     T foo(T a){ return a; }
}
mixin Foo!int g;
mixin Foo!string g;

void main(){
     writeln(foo(2));
     writeln(foo("a"));
     writeln(g.foo(2));
     writeln(g.foo("a"));
}

// -----

import std.stdio;

namespace g{
     int foo(int a){ return a; }
}
namespace g{
     string foo(string a){ return a; }
}

void main(){
     writeln(foo(2));
     writeln(foo("a"));
     writeln(g.foo(2));
     writeln(g.foo("a"));
}

Both examples should still work if the two mixins/namespaces occur in 
(possibly different) imported modules. I think this would be in line 
with how lookup is generally handled in D. (Note that I am not 
suggesting to make namespaces extensible, but rather to make them 
overloadable.) How do you think about this?



More information about the Digitalmars-d mailing list