www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When should we use Modules and when Should we use Classes?

reply BoQsc <vaidas.boqsc gmail.com> writes:
Would be nice to have a short summary or detailed answer on this.

Some resources to discuss this topic:
https://dlang.org/spec/class.html
https://dlang.org/spec/module.html
Sep 18 2019
parent Adam D. Ruppe <destructionator gmail.com> writes:
You can only have one instance of a module, so it is really more 
a collection of types, functions, etc. that help each other do a 
particular job.

Then structs and classes can have multiple objects of each type 
so you use them to do most the stuff you run. You can make class 
objects in a loop or array, you can make temporary ones, 
replacable ones, etc. Modules cannot do any of that.

So in short:

* use a module to group classes and functions
* use a class when you need to subclass it and customize 
functionality and/or need several instances
* use a struct for most other cases, it is a generic collection 
of data
Sep 18 2019