Working with modules

Jonathan M Davis jmdavisProg at gmx.com
Fri Feb 15 23:45:51 PST 2013


On Friday, February 15, 2013 20:14:41 Ali Çehreli wrote:
> On 02/15/2013 07:50 PM, Jeremy DeHaan wrote:
>  > I know that a module can only be defined once, but I feel like there
>  > could be times where it would be helpful to be able to have the same
>  > module defined in separate files
> 
> There may be other ways of achieving that and I haven't given much
> thought to your question but there is also the import expression:
> 
>    http://dlang.org/expression.html#ImportExpression
> 
> If needed, you can include D files similar to C's #include:
> 
> mixin (import ("part_of_my_module.d"));
> mixin (import ("another_part_of_my_module.d"));

You can play games like this if you really want to, but in general, it should 
be kept in mind that modules in D are designed to have a one-to-one 
correspondance with files, and packages are designed to have a one-to-one 
correspondance with directories. And in general, fighting that is just going to 
cause trouble and confusion. That doesn't necessarily mean that it should 
never be done, but in most cases, it would be far better to just reorganize 
your code so that it's not necessary. Also, AFAIK, mixing in imports is a very 
rare thing to do, so for the most part, people won't be expecting it, and it 
will likely cause maintenance issues for anyone else working on your project.

What is more typically done when you need to split stuff up is to use internal 
modules which are only imported internally and are not presented as part of 
the public API, but even that isn't terribly common, I don't think (but unlike 
mixing in imports, it's something that the standard library actually does in a 
few places).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list