Struct Interface Implementation?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 12 18:28:13 PDT 2011


On 2011-06-12 18:20, Mehrdad wrote:
> Is there any particular reason that structs and unions can't implement
> interfaces?
> 
> If that was possible, template conditions could become much simpler
> for a variety of cases, such as for checking if something is a
> particular type of range or container.
> 
> Thoughts?

They aren't virtual. For an interface to work, it has to be virtual. structs 
are value types, not reference types. They have no virtual table and have no 
polymorphism.

At best, there could be a way to indicate that a struct happens to have 
functions which match what the interface has, which could then be used for 
static instrospection, but you could never actually use a struct as an 
interface. But there is not currently any way to use an interface to check 
whether a struct implements a particular set of functions. And while eponymous 
templates such as isForwardRange could be simplified with such a feature, 
their usage would be the same, so it might help, but it wouldn't really help 
with template constraints. Not to mention, interfaces are likely to be far 
stricter about types than templates are, so it might be difficult to get that 
to work generically. Regardless, I'm very leery of associating interfaces and 
structs in any way shape or form, because they are _very_ different things.

- Jonathan M Davis


More information about the Digitalmars-d mailing list