September 18, 2019 When should we use Modules and when Should we use Classes? | ||||
---|---|---|---|---|
| ||||
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 |
September 18, 2019 Re: When should we use Modules and when Should we use Classes? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | 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 |
Copyright © 1999-2021 by the D Language Foundation