structure inheritance and polymorphism

IGotD- nise at nise.com
Wed Jan 6 17:58:24 UTC 2021


On Monday, 4 January 2021 at 01:06:20 UTC, zjh wrote:
> Can d be added with support for structure inheritance and 
> polymorphism?
> I like D, but I don't like GC, so I can't use classes. I can 
> only use betterC. But I want d to have c++ structure 
> inheritance and polymorphism. In this way, I think the 
> competition with c++, the odds are better.

I think that mixin templates is something you should look at. It 
is not inheritance but composition. Unfortunately mixin templates 
are badly documented explaining how they can be used as an 
alternative to inheritance or alias this.

Basically one way to emulate polymorphism is to use opCast, 
returning an inner member variable. The implicit cast like 
"operator Type()" in C++ is not available in D to begin with so 
the casting must be stated anyway.

Keep in mind that you can use classes without GC. For exampled 
using the scoped macro or manually allocated.


More information about the Digitalmars-d mailing list