Bug 16532 – Add "namespace" Keyword?

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-24T03:35:03Z
Last change time
2020-03-21T03:56:32Z
Assigned to
No Owner
Creator
Jack

Comments

Comment #0 by gyroheli — 2016-09-24T03:35:03Z
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.
Comment #1 by b2.temp — 2016-09-24T06:57:44Z
Instead of a struct you can use a template without parameter. The compatibility with C++ is not a valid argument because namespaces are supported: https://dlang.org/spec/cpp_interface.html#cpp-namespaces.
Comment #2 by gyroheli — 2016-10-21T14:59:28Z
Exactly, name spaces are supported but only through an extern. If I want to write D code with namespaces it isn't possible. Hence a lack of completeness. You can't use a template without a parameter? template Name() { enum a = 2; enum b = 3; } writeln(Name.a, Name.b); // error Name has no property a or b
Comment #3 by b2.temp — 2016-10-21T20:31:26Z
(In reply to Jack from comment #2) > Exactly, name spaces are supported but only through an extern. If I want to > write D code with namespaces it isn't possible. Hence a lack of completeness. > > You can't use a template without a parameter? > > template Name() > { > enum a = 2; > enum b = 3; > } > > writeln(Name.a, Name.b); // error Name has no property a or b You must alias it to create an instance. alias name = Name!(); and in the code use this alias. But you know, namespaces will never be added. D has modules, end of story. Ask on the news group and you'll see...You can even close this issue.
Comment #4 by mathias.lang — 2016-10-22T16:22:05Z
> 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. First, this is an enhancement proposal, thus it should go through the DIP process: https://github.com/dlang/DIPs/ Second, namespacing in D are supported through module / packages. You can choose to introduce extra namespacing without introducing new modules/packages by using aggregates as already mentioned, but it sounds like your real problem is trying to code with a C++ approach in D.