Usage preference between a struct and a class in D language

Reason #1 to choose struct vs class: classes have inheritance, structs do not. If you need polymorphism, you must use classes.

Reason #2: structs are normally value types (though you can make them reference types if you work at it). Classes are always reference types. So, if you want a value type, choose a struct. If you want a reference type, it’s easiest to go with a class.

Reason #3: If you have a type with a lot of data members, then you’re probably going to want a reference type (to avoid expensive copying), in which case, you’re probably going to choose a class.

Reason #4: If you want deterministic destruction of your type, then it’s going to need to be a struct on the stack. Nothing on the GC heap has deterministic destruction, and the destructiors/finalizers of stuff on the GC heap may never be run. If they’re collected by the GC, then their finalizers will be run, but otherwise, they won’t. So, if you want your type to automatically be destroyed when it leaves scope, you need to use a struct and put it on the stack.

As for your particular case, containers should normally be reference types (copying all of their elements every time that you pass one around would be insanely expensive), and a Stack is a container, so you’re going to want to use a class unless you want to go to the trouble of making it a ref-counted struct, which is decidedly more work. It just has the advantage of guaranteeing that its destructor will run when it’s not used anymore.

On a side note, if you create a container which is a class, you’re probably going to want to make it final so that its various functions can be inlined (and won’t be virtual if that class doesn’t derive from anything other than Object and they’re not functions that Object has), which can be important for something like a container where performance can definitely matter.

Leave a Comment