www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are modules analogous to namespaces in C# or packages in Java?

reply "Afshin" <davidnia1 gmail.com> writes:
Modules confuse me as a way to organize code.

If a module is composed of multiple classes, it seems that the 
module semantics in D encourages putting all those classes in one 
file.

Can someone explain how to organize a set of classes into a 
module (or namespace, or package) so that each class can have 
their own file?
Jan 01 2014
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Wed, 01 Jan 2014 17:43:53 +0000, Afshin wrote:

 Modules confuse me as a way to organize code.
 
 If a module is composed of multiple classes, it seems that the module
 semantics in D encourages putting all those classes in one file.
 
 Can someone explain how to organize a set of classes into a module (or
 namespace, or package) so that each class can have their own file?
Read about modules here: http://dlang.org/module.html Modules are not like namespaces: "Modules have a one-to-one correspondence with source files." A module cannot span multiple files. You may organize code into modules however you like; idiomatically for D, code is grouped into modules by functionality (e.g. std.algorithm) and may contain many classes, structures, and free functions. While modules cannot be defined across multiple files, they do nest; you could have a file structure like this: src/ myproject/ core/ class1.d class2.d With the following modules: myproject.core.class1, myproject.core.class2 A package.d module would allow other code to simply import myproject.core, accomplishing a similar effect.
Jan 01 2014
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Afshin:

 Can someone explain how to organize a set of classes into a 
 module (or namespace, or package) so that each class can have 
 their own file?
Lot of D code is not made of classes :-) There are also lot of free functions, some templates, some type definitions, some interfaces, some global constants or variables, module constructors, and more. Often don't put a single class in each module. Generally you put related stuff in a single module. Bye, bearophile
Jan 01 2014
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On 1/2/2014 2:43 AM, Afshin wrote:
 Modules confuse me as a way to organize code.

 If a module is composed of multiple classes, it seems that the module
 semantics in D encourages putting all those classes in one file.

 Can someone explain how to organize a set of classes into a module (or
 namespace, or package) so that each class can have their own file?
Just think of a module as a source file, much like a Java source file. You can think of foo.d as being the same as foo.java. Modules (D source files) can be organized into packages, very much like Java source files can. So if you have a file bar/baz/foo.d, you can add this at the top: module bar.baz.foo; This means package bar contains a subpackage baz which contains a module foo. You can organize your modules any way you'd like, so if you want to put one class in each module, that's perfectly fine: ============== // bar/baz/foo.d module bar.baz.foo; class Foo {} // bar/baz/borg.d module bar.baz.borg; class Borg {} ============== Unlike Java source files, D source files (modules) allow you to place variable declarations and free functions at module-scope. They can be public, package, or private, just like class members and methods. ============== module bar.baz.foo; interface Foo {} private fooCount; Foo makeFoo( int someFlag;) { ++fooCount; if( someFlag ) return new FooImpl1; else return new FooImpl2; } private class FooImpl1 : Foo {} private class FooImpl2 : Foo {} ============== In D, private class methods and variables are visible inside the module in which they are declared. This can be very handy. For example, if FooImpl1 and FooImpl2 have private methods, other functions and class mehtods in bar.baz.foo can call them directly. This is one of the reasons I prefer in my own code not to use one class per module. I group modules based on related functionality. I may have a variety of free functions, classes, structs, enums, and private variables in a single module. But I do try to keep each module to a reasonable size. I don't like seeing thousands of lines in a single module. Choosing the granularity for separating functionality into separate modules is just a matter of taste. Ultimately, D gives you a lot of freedom in how to organize your module layout.
Jan 02 2014
prev sibling parent "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Wednesday, 1 January 2014 at 17:43:54 UTC, Afshin wrote:
 Modules confuse me as a way to organize code.

 If a module is composed of multiple classes, it seems that the 
 module semantics in D encourages putting all those classes in 
 one file.

 Can someone explain how to organize a set of classes into a 
 module (or namespace, or package) so that each class can have 
 their own file?
Java is not (yet) truly modular programming language. Java 8 is supposed to include Jigsaw (module system for Java). More about it: http://openjdk.java.net/projects/jigsaw/ Packages in D are similar to packages in Java. Concept of modules as I wrote above, does not yet exist in Java.
Jan 02 2014