DIP61: Add namespaces to D

Johannes Pfau via Digitalmars-d digitalmars-d at puremagic.com
Sat Apr 26 04:56:56 PDT 2014


Am Sat, 26 Apr 2014 02:31:51 -0700
schrieb Walter Bright <newshound2 at digitalmars.com>:

> http://wiki.dlang.org/DIP61
> 
> 
> but it seems to me that the simplest, most straightforward approach
> would be better.
> 
> As more and more people are attempting to call C++ libraries from D,
> this is getting to be a more and more important issue.

As the namespace keyword only makes sense for c++ interfacing code it
could make sense to implement this as a compiler-recognized UDA:

Benefits:
  * No new keyword, so absolutely no code breakage for old code
    (namespace is likely used in some parser code as an identifier)
  * @namespace is only available when explicitly importing core.cpp
    which makes sense imho
  * No new syntax / parser changes

The rest of the DIP wouldn't be affected in any way.

---------------------------------
module core.cpp; //core.attribute, or any clever name
struct namespace
{
    string ns;
}
---------------------------------

In user code:
---------------------------------
import core.cpp;

@namespace("N")
{
    int foo(); int bar();
}
---------------------------------

Example:
http://dpaste.dzfl.pl/4d583dac89dc


Another small nitpick:
The DIP does not restrict namespaces to C++ interfacing code.
I think we should avoid having namespaces and modules in D, this will
only confuse new users and lead to bad code. We should at least state
this in the DIP or only allow the namespace keyword in extern(C++)
blocks.


More information about the Digitalmars-d mailing list