On Monday, 1 September 2025 at 13:58:23 UTC, Brother Bill wrote:
I have heard that there are better or at least alternative ways
to have encapsulation, polymorphism and inheritance outside of
OOP.
With OOP in D, we have full support for Single Inheritance,
including for Design by Contract (excluding 'old'). D also
supports multiple Interfaces.
D supports several different kinds of polymorphism, which vary
based on (a) whether dispatch occurs at compile-time or runtime,
and (b) whether the set of implementations is closed or open.
- **Function overloading** enable compile-time dispatch with a
closed set of implementations.
- **Templates** enable compile-time dispatch with an open set of
implementations.
- **Sum types** enable runtime dispatch with a closed set of
implementations.
- **Classes** enable runtime dispatch with an open set of
implementations.
Generally speaking, when choosing which type of polymorphism to
use, I try to follow the [rule of least power][1]. Since
compile-time dispatch is less powerful than runtime dispatch, and
a closed set of implementations is less powerful than an open
set, that means preferring function overloads when possible, then
templates or sum types, and finally classes when neither of the
other options will suffice.
[1]: https://en.wikipedia.org/wiki/Rule_of_least_power