How to let D's one module contains lots of classes.

Mike Parker via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 9 03:24:46 PST 2014


On 11/9/2014 7:25 PM, AlanThinker wrote:
> On Sunday, 9 November 2014 at 10:02:41 UTC, bearophile wrote:
>> AlanThinker:
>>
>>> If there all classes in one file, the file will be to big.
>>
>> The file will also probably contain several free functions, and not
>> just classes.
>>
>> Bye,
>> bearophile
>
> Is it possible to let one module contains lots of classes in different
> files, and free functions can live in one or several files.

Don't think of a module as directly equivalent to a C# namespace. It is 
not. A module is a single file, nothing more. You can use packages to 
group modules in multiple namespaces. If you want to have a namespace 
foo.gui with a single class per module, then here's a working example of 
something you could do:


################
// test/foo/gui/button.d
module foo.gui.button;

class Button
{
     public this() {
         import std.stdio : writeln;
         writeln( "New Button!" );
     }
}
// test/foo/gui/widget.d
module foo.gui.widget;

class Widget {
     public this() {
         import std.stdio : writeln;
         writeln( "New Widget!" );
     }
}
// test/foo/gui/package.d
module foo.gui;

public import foo.gui.button, foo.gui.widget;

// test/foo/namespace.d
module namespace;

import foo.gui;

void main()
{
     auto w = new foo.gui.Widget;
     auto b = new foo.gui.Button;
}

###############

cd test
dmd namespace.d foo/gui/package.d foo/gui/widget.d foo/gui/button.d



More information about the Digitalmars-d mailing list