Alternatives to OOP in D

Steven Schveighoffer schveiguy at gmail.com
Mon Sep 1 22:45:58 UTC 2025


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.
>
> What would be the alternatives and why would they be better?
> I assume the alternatives would have
> 1. Better performance
> 2. Simpler syntax
> 3. Easier to read, write and maintain
>
> If possible, please provide links to documentation or examples.

I have heard that people use structs and do these things 
themselves. I know of one library that implements this: 
https://code.dlang.org/packages/tardy

IMO, the largest problem with classes for people is that it's 
always a reference type, and generally heap (gc) allocated. So 
performance and expressiveness. IMO, reference type is the 
correct default for polymorphism.

And also note that structs already support encapsulation. They 
just don't do inheritance/polymorphism.

I believe last year's dconf Walter hinted that he would be open 
to adding struct inheritance/interfaces.

I think roll-your-own polymorphism is always going to suck 
compared to language-supplied.

-Steve


More information about the Digitalmars-d-learn mailing list