Thread overview
Code-File structure
Jun 12, 2012
Francois Chabot
Jun 13, 2012
Kagamin
Jun 13, 2012
Kagamin
Jun 13, 2012
Nathan M. Swan
Jun 14, 2012
Pragma Tix
Jun 14, 2012
Artur Skawina
Jun 14, 2012
Pragma Tix
Jun 14, 2012
Artur Skawina
June 12, 2012
Hi!

I'm new to D and trying everything out. Got the basics down which are very straightforward and similar to other languages. My professional background is primarily in C/C++-variants and Ruby if it helps.

I have a problem with how modules, packages and files work. I don't really know how I am supposed to organize my code. I like namespaces from C++ which is probably my curse. What I want to write is code similar to this:

MyClass obj = new MyClass();
SomePackage.SecondClass secondObj = new SomePackage.SecondClass();

Problem here is that SecondClass is a module. Is there some nifty trick I can do here to solve this? I tried with just having one module file which would hold all it's classes and functions but that backfired very quickly as you can imagine since it grew too large too quickly for most simple things.

I am open to any suggestions of course which would make it simple for me to have a good structure with my files and also make my code easily readable. Though if possible I would like anything that goes under the package/module can somehow be placed in it's own folder.
June 12, 2012
On Tuesday, 12 June 2012 at 07:04:15 UTC, Henrik Valter Vogelius Hansson wrote:
> Hi!
>
> I'm new to D and trying everything out. Got the basics down which are very straightforward and similar to other languages. My professional background is primarily in C/C++-variants and Ruby if it helps.
>
> I have a problem with how modules, packages and files work. I don't really know how I am supposed to organize my code. I like namespaces from C++ which is probably my curse. What I want to write is code similar to this:
>
> MyClass obj = new MyClass();
> SomePackage.SecondClass secondObj = new SomePackage.SecondClass();
>
> Problem here is that SecondClass is a module. Is there some nifty trick I can do here to solve this? I tried with just having one module file which would hold all it's classes and functions but that backfired very quickly as you can imagine since it grew too large too quickly for most simple things.
>
> I am open to any suggestions of course which would make it simple for me to have a good structure with my files and also make my code easily readable. Though if possible I would like anything that goes under the package/module can somehow be placed in it's own folder.


You can get the equivalent of C++ namespaces with the following project structure:

project_folder
   - SomePackage.d
   - SomePackage_impl
       -SecondClass.d


and then in SomePackage.d:
  module SomePackage;
  public import SomePackage_impl.SecondClass;

June 12, 2012
On Tuesday, 12 June 2012 at 18:07:45 UTC, Francois Chabot wrote:
> On Tuesday, 12 June 2012 at 07:04:15 UTC, Henrik Valter Vogelius Hansson wrote:
>> Hi!
>>
>> I'm new to D and trying everything out. Got the basics down which are very straightforward and similar to other languages. My professional background is primarily in C/C++-variants and Ruby if it helps.
>>
>> I have a problem with how modules, packages and files work. I don't really know how I am supposed to organize my code. I like namespaces from C++ which is probably my curse. What I want to write is code similar to this:
>>
>> MyClass obj = new MyClass();
>> SomePackage.SecondClass secondObj = new SomePackage.SecondClass();
>>
>> Problem here is that SecondClass is a module. Is there some nifty trick I can do here to solve this? I tried with just having one module file which would hold all it's classes and functions but that backfired very quickly as you can imagine since it grew too large too quickly for most simple things.
>>
>> I am open to any suggestions of course which would make it simple for me to have a good structure with my files and also make my code easily readable. Though if possible I would like anything that goes under the package/module can somehow be placed in it's own folder.
>
>
> You can get the equivalent of C++ namespaces with the following project structure:
>
> project_folder
>    - SomePackage.d
>    - SomePackage_impl
>        -SecondClass.d
>
>
> and then in SomePackage.d:
>   module SomePackage;
>   public import SomePackage_impl.SecondClass;

Nice thanks! That will help me out a lot!
Is there a "D" way to do it? Or is D too young to have that yet?

You know Ruby has a "Ruby" way to do stuff. Like using blocks to
do some things instead of the traditional way in C style
languages.
June 13, 2012
On Tuesday, 12 June 2012 at 07:04:15 UTC, Henrik Valter Vogelius Hansson wrote:
> Hi!
>
> I'm new to D and trying everything out. Got the basics down which are very straightforward and similar to other languages. My professional background is primarily in C/C++-variants and Ruby if it helps.
>
> I have a problem with how modules, packages and files work. I don't really know how I am supposed to organize my code. I like namespaces from C++ which is probably my curse. What I want to write is code similar to this:
>
> MyClass obj = new MyClass();
> SomePackage.SecondClass secondObj = new SomePackage.SecondClass();
>
> Problem here is that SecondClass is a module. Is there some nifty trick I can do here to solve this? I tried with just having one module file which would hold all it's classes and functions but that backfired very quickly as you can imagine since it grew too large too quickly for most simple things.
>
> I am open to any suggestions of course which would make it simple for me to have a good structure with my files and also make my code easily readable. Though if possible I would like anything that goes under the package/module can somehow be placed in it's own folder.

A module is a file, which can contain multiple functions/classes/etc. A package is a folder with files. You cannot put multiple modules in a file like C++ namespaces. Generally, I create multiple modules in a single package, and only use multiple packages when I have too many modules.

Here's a sample file structure:

src
  - ddi
    - color.d
    - csv.d
    - io.d
    - main.d
    - msg.d

A larger project, SDC, has 15 modules in the main package, and six subpackages with on average five modules inside them. I like the rule-of-15 in whether something is too big, though that is personal taste and you might like more structure than I do.

In this case, have SomePackage be some_module, and have SecondClass be inside of some_module.d:

auto obj = new MyClass();
auto secondObj = new some_module.SecondClass();

It's convention to _always_ use lower case, due to different filesystem's rules on case.

Hope this helps,
NMS
June 13, 2012
On Tuesday, 12 June 2012 at 19:35:47 UTC, Henrik Valter Vogelius Hansson wrote:
> Nice thanks! That will help me out a lot!
> Is there a "D" way to do it? Or is D too young to have that yet?

The D way is to have all classes in one file.
June 13, 2012
On Wednesday, 13 June 2012 at 15:15:11 UTC, Kagamin wrote:
> On Tuesday, 12 June 2012 at 19:35:47 UTC, Henrik Valter Vogelius Hansson wrote:
>> Nice thanks! That will help me out a lot!
>> Is there a "D" way to do it? Or is D too young to have that yet?
>
> The D way is to have all classes in one file.

As an alternative simply import the module where the SecondClass resides, it should just work.
June 14, 2012
Am 13.06.2012 03:56, schrieb Nathan M. Swan:
> You cannot put multiple modules in a file like C++ namespaces.

A nice trick in D for creating local namespaces that combine items from several modules under one name is to use a mixin template.

module app;

mixin template StdLib()
{
   import std.string;
   import std.stdio;
}

mixin StdLib!() stdlib;

void main()
{
	int x,y =1;
	stdlib.writefln("hello");
	string s = stdlib.format("%s, %s", x,y);
	stdlib.writefln(s);
}

untested..
June 14, 2012
On 06/14/12 19:26, Pragma Tix wrote:
> Am 13.06.2012 03:56, schrieb Nathan M. Swan:
>> You cannot put multiple modules in a file like C++ namespaces.
> 
> A nice trick in D for creating local namespaces that combine items from several modules under one name is to use a mixin template.
> 
> mixin template StdLib()
> {
>    import std.string;
>    import std.stdio;
> }
> 
> mixin StdLib!() stdlib;

The 'mixin' part is not necessary:

   template StdLib() {
      import std.string;
      import std.stdio;
   }
   alias StdLib!() stdlib;

but you don't even need a template:

   struct stdlib { import std.string, std.stdio; }


artur
June 14, 2012
Am 14.06.2012 20:01, schrieb Artur Skawina:
> On 06/14/12 19:26, Pragma Tix wrote:
>> Am 13.06.2012 03:56, schrieb Nathan M. Swan:
>>> You cannot put multiple modules in a file like C++ namespaces.
>>
>> A nice trick in D for creating local namespaces that combine items from several modules under one name is to use a mixin template.
>>
>> mixin template StdLib()
>> {
>>     import std.string;
>>     import std.stdio;
>> }
>>
>> mixin StdLib!() stdlib;
>
> The 'mixin' part is not necessary:
>
>     template StdLib() {
>        import std.string;
>        import std.stdio;
>     }
>     alias StdLib!() stdlib;
>
> but you don't even need a template:
>
>     struct stdlib { import std.string, std.stdio; }
>
>
> artur

That's pretty cool. thanks.  Is this struct {import ...}feature documented.. somewhere ?
June 14, 2012
On 06/14/12 22:03, Pragma Tix wrote:
> Am 14.06.2012 20:01, schrieb Artur Skawina:
>> On 06/14/12 19:26, Pragma Tix wrote:
>>> Am 13.06.2012 03:56, schrieb Nathan M. Swan:
>>>> You cannot put multiple modules in a file like C++ namespaces.
>>>
>>> A nice trick in D for creating local namespaces that combine items from several modules under one name is to use a mixin template.
>>>
>>> mixin template StdLib()
>>> {
>>>     import std.string;
>>>     import std.stdio;
>>> }
>>>
>>> mixin StdLib!() stdlib;
>>
>> The 'mixin' part is not necessary:
>>
>>     template StdLib() {
>>        import std.string;
>>        import std.stdio;
>>     }
>>     alias StdLib!() stdlib;
>>
>> but you don't even need a template:
>>
>>     struct stdlib { import std.string, std.stdio; }
>>
>>
>> artur
> 
> That's pretty cool. thanks.  Is this struct {import ...}feature documented.. somewhere ?

It's how imports work; a struct creates a named scope, so does a template. "Scoped" imports are documented: http://dlang.org/module.html

artur