Seperating class methods in a different module

Neia Neutuladh neia at ikeran.org
Thu Dec 7 02:50:29 UTC 2017


On Thursday, 7 December 2017 at 02:32:03 UTC, helxi wrote:
> 1. How can I separate class methods from the declaration block? 
> And how can I implement them in a separate module?

module a;
class Test
{
   import b;
   mixin TestMethodImpl!();
}

module b;
template TestMethodImpl()
{
   void foo();
}

But this is abnormal for D. We almost invariably put function 
bodies inside declarations.

What's your goal with separating the declaration from the 
implementation?

If you want people to be *unable* to read method bodies, you can 
generate *.di files that only contain declarations. `dmd --help | 
grep -i header` or so -- though that keeps in templates and 
functions that are candidates for inlining.

If you want to make it easy for someone to look at the API 
without wading through function declarations, use ddox to 
generate HTML documentation.

> Imagine I am making a package called forward_list. Under the 
> directory forward_list/, I have modules.d that imports 
> dedicated modules for useful methods like push_back, 
> insert_after, front, sort, merge etc. Is it an acceptable 
> practice to declare classes in package.d?

That would be abnormal.

You can look at the layout of std.container by way of comparison 
-- one module per container type, one for miscellaneous 
utilities, and only documentation in package.d.


More information about the Digitalmars-d-learn mailing list