www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to organize using modules?

reply "Afshin" <davidnia1 gmail.com> writes:
Is it possible to describe modules in terms of packages (as found 
in Java)?

The features that Java packages have that I can't seem to get in 
D are:
1) You can have classes that are in the same package, but kept in 
separate files.
2) You can import many classes using the '*' syntax.

Is this possible in D?

What I understand is that if I have ClassA and ClassB in Module1, 
and I want to keep the classes in separate files, then I have to 
use the following module statements:

in ClassA:
module Module1.ClassA;

in ClassB:
module Module1.ClassB;

But now it becomes cumbersome to use the classes because now I 
have to import them explicitely:

import Module1.ClassA;
import Module1.ClassB;



If I wanted to use:
import Module1;

Then it seems I have to have ClassA and ClassB in the same D file.

Am I missing something?
Dec 30 2013
next sibling parent reply "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
On Tuesday, 31 December 2013 at 06:01:21 UTC, Afshin wrote:
 Is it possible to describe modules in terms of packages (as 
 found in Java)?

 The features that Java packages have that I can't seem to get 
 in D are:
 1) You can have classes that are in the same package, but kept 
 in separate files.
 2) You can import many classes using the '*' syntax.

 Is this possible in D?

 What I understand is that if I have ClassA and ClassB in 
 Module1, and I want to keep the classes in separate files, then 
 I have to use the following module statements:

 in ClassA:
 module Module1.ClassA;

 in ClassB:
 module Module1.ClassB;

 But now it becomes cumbersome to use the classes because now I 
 have to import them explicitely:

 import Module1.ClassA;
 import Module1.ClassB;



 If I wanted to use:
 import Module1;

 Then it seems I have to have ClassA and ClassB in the same D 
 file.

 Am I missing something?
It is possible if you have your file system set up like this. Module1 -ClassA.d -ClassB.d -package.d //In Module1/ClassA.d module Module1.ClassA; class ClassA { ... } //In Module1/ClassB.d module Module1.ClassB; class ClassB { ... } //In Module1/package.d module Module1; public import Module1.ClassA; public import Module1.ClassB; //In main.d module main; import Module1; //This will import both ClassA and ClassB. Very important! The name of the file with the public imports must be package.d
Dec 30 2013
parent "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
 2) You can import many classes using the '*' syntax.
You don't use the * syntax. Just stop after the module name.
Dec 30 2013
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-12-31 07:01, Afshin wrote:
 Is it possible to describe modules in terms of packages (as found in Java)?

 The features that Java packages have that I can't seem to get in D are:
 1) You can have classes that are in the same package, but kept in
 separate files.
See below.
 2) You can import many classes using the '*' syntax.
You cannot do that in D. You can do something similar as described here: http://forum.dlang.org/thread/garueoxkjjqgqpqqhrmc forum.dlang.org#post-sdueirvfgsxjtahmapla:40forum.dlang.org
 Is this possible in D?

 What I understand is that if I have ClassA and ClassB in Module1, and I
 want to keep the classes in separate files, then I have to use the
 following module statements:

 in ClassA:
 module Module1.ClassA;

 in ClassB:
 module Module1.ClassB;

 But now it becomes cumbersome to use the classes because now I have to
 import them explicitely:

 import Module1.ClassA;
 import Module1.ClassB;
Yes, that's how it works in D. That's because in Java there's a one-to-one mapping of classes and files. In D you can have many classes (or other declarations) in the same file. I suggest you use this approach.
 If I wanted to use:
 import Module1;

 Then it seems I have to have ClassA and ClassB in the same D file.

 Am I missing something?
-- /Jacob Carlborg
Dec 31 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Afshin:

 in ClassA:
 module Module1.ClassA;

 in ClassB:
 module Module1.ClassB;
In D module names usually start with a lowercase letter (usually they are all lowercase, because different file systems manage upper case letters in different ways). Bye, bearophile
Dec 31 2013