structs inheriting from and implementing interfaces

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Dec 29 12:50:24 UTC 2017


On Friday, December 29, 2017 12:18:57 Mike Franklin via Digitalmars-d-learn 
wrote:
> On Friday, 29 December 2017 at 12:11:46 UTC, rikki cattermole
>
> wrote:
> > Structs are structs, classes are classes.
>
> I'm talking about interfaces, which are neither structs nor
> classes.

Interfaces are related to classes and not structs. Structs do not have
inheritance and do not implement interfaces in any way shape or form.
Classes and interfaces are always references, whereas structs never are, and
structs do not have virtual functions. Structs are either directly placed
where they are (be it on the stack or inside an object that contains them),
or they're placed on the heap and accessed via a pointer. As such, in D,
structs are fundamentally different from classes or interfaces.

If you want a function to accept multiple types of structs or classes which
share the same API, then use templates and use the template constraint to
restrict what the template accepts. That's what's done with ranges (e.g.
with isInputRange and isForwardRange).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list