[Issue 16532] New: Add "namespace" Keyword?
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Fri Sep 23 20:35:03 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16532
Issue ID: 16532
Summary: Add "namespace" Keyword?
Product: D
Version: D2
Hardware: All
URL: http://dlang.org/
OS: All
Status: NEW
Severity: enhancement
Priority: P3
Component: dmd
Assignee: nobody at puremagic.com
Reporter: gyroheli at gmail.com
Well namespaces are already implemented, but right now they are restricted to
modules. So a namespace can be created in the sense like this:
// file: other/package.d
import Name = other.modul;
// file: other/modul.d
module other.modul;
enum someConstant = 10;
// end -----------------------------
so now it would be accessed like: Name.someConstant. This is fine but this
involves putting everything into separate files. So if you want different
namespaces that means making a bunch of different files, maybe sometimes even
having just a single "enum" inside of the file. being able to have them in one
file would be better and it is possible using structs as well.
struct Name
{
@disable this(); // bloat that shouldn't exist
@disable this(this);
enum someConstant = 10;
}
struct OtherName
{
@disable this();
@disable this(this);
enum moreConstant = 20;
}
struct EtcName
{
@disable this();
@disable this(this);
enum etcConstant = 000;
}
Even with the @disable it is still possible to declare a variable like so:
Name* ops; // shouldn't be possible
It also adds extra declarations like "Name.init" and "Name.sizeof" all of which
is not needed.
Really namespaces already exist in D, just they are very crudly implemented by
either meaning horrible to maintain with a bunch of extra module files or
through the use of structs. Adding namespaces would just be for sanity so that
code like above isn't possible and so that namespaces are treated as such. As
well adding namespace keyword will also help port from C++ if there is any code
that needs it. Just a thought for a small improvement.
--
More information about the Digitalmars-d-bugs
mailing list