www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Struct Interface Implementation?

reply Mehrdad <wfunction hotmail.com> writes:
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?
Jun 12 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jun 12 2011
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 6/12/11 8:28 PM, Jonathan M Davis wrote:
 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.
He must be referring to nominal conformance. It's been discussed many times. There are important disadvantages (e.g. you can't "implement" a type alias) but there are advantages too. Ultimately we never got around to it. Andrei
Jun 12 2011
parent reply Mehrdad <wfunction hotmail.com> writes:

lack of boxing).

On 6/12/2011 6:46 PM, Andrei Alexandrescu wrote:
 On 6/12/11 8:28 PM, Jonathan M Davis wrote:
 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.
still work pretty well (e.g. foreach loops work with List<T>.Enumerator, which is a struct but which can be treated as the IEnumerator<T> interface), right?
 He must be referring to nominal conformance. It's been discussed many 
 times. There are important disadvantages (e.g. you can't "implement" a 
 type alias) but there are advantages too.
 Ultimately we never got around to it.

 Andrei
Haha ok, that's a good reason in its own way, thanks. :)
Jun 12 2011
parent Max Klyga <max.klyga gmail.com> writes:
On 2011-06-13 06:21:47 +0300, Mehrdad said:


 the lack of boxing).
 
 On 6/12/2011 6:46 PM, Andrei Alexandrescu wrote:
 On 6/12/11 8:28 PM, Jonathan M Davis wrote:
 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.
they still work pretty well (e.g. foreach loops work with List<T>.Enumerator, which is a struct but which can be treated as the IEnumerator<T> interface), right?
autoboxing structs in objects to allow Interface usage, calling Object methods or some magic duck typing. In most cases it devirtualizes calls, so there is no overhead in boxing.
 
 He must be referring to nominal conformance. It's been discussed many 
 times. There are important disadvantages (e.g. you can't "implement" a 
 type alias) but there are advantages too.
 Ultimately we never got around to it.
 
 Andrei
Haha ok, that's a good reason in its own way, thanks. :)
Jun 13 2011