Module-based programming
Jacob Carlborg
doob at me.com
Fri Jul 26 03:02:25 PDT 2013
On 2013-07-26 11:12, Land wrote:
> I'm confused when it comes to modules.
>
> I've read somewhere that modules are basically 'singleton
> classes' and that anything that doesn't need its own state should
> not needlessly be put inside a class.
Modules are not singleton classes. They haven't much to do with classes
at all. I mean, you cannot inherit modules.
> But what if I want to create a simple OpenGL shader object. Do I
> create a class like this:
>
> class Shader
> {
> // Method
> // Method
> // Method
> // Method
>
> // Field
> // Field
> // Field
> }
>
> Or do I have a structure that I operate on via module methods?
> (C-style, basically)
>
> struct Shader
> {
> // Field
> // Field
> // Field
> }
>
> // Method
> // Method
> // Method
> // Method
>
> It's probably clear to you guys, but I'm stumped.
>
> Thanks for any answers
In general I would say go with a class, but since you mentioned OpenGL
it probably needs to go very fast and minimize allocations. In that case
a struct would probably be better, if you're going to use a lot of these
Shaders.
It also depends on if you need value semantics or reference semantics
for Shader. Classes always have reference semantics, structs have value
semantics by default, and if you use a pointers you'll get reference
semantics.
Note that you can have methods in a struct too. So in any case, I would
say, don't put the functions at module scope.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list