digitalmars.D - Struct Interface Implementation?
- Mehrdad (6/6) Jun 12 2011 Is there any particular reason that structs and unions can't implement
- Jonathan M Davis (16/24) Jun 12 2011 They aren't virtual. For an interface to work, it has to be virtual. str...
- Andrei Alexandrescu (6/18) Jun 12 2011 He must be referring to nominal conformance. It's been discussed many
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
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
On 6/12/11 8:28 PM, Jonathan M Davis wrote:On 2011-06-12 18:20, Mehrdad wrote: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. AndreiIs 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.
Jun 12 2011
lack of boxing). On 6/12/2011 6:46 PM, Andrei Alexandrescu wrote:On 6/12/11 8:28 PM, Jonathan M Davis wrote: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?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. AndreiHaha ok, that's a good reason in its own way, thanks. :)
Jun 12 2011
On 2011-06-13 06:21:47 +0300, Mehrdad said:the lack of boxing). On 6/12/2011 6:46 PM, Andrei Alexandrescu wrote: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.On 6/12/11 8:28 PM, Jonathan M Davis wrote: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?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. AndreiHaha ok, that's a good reason in its own way, thanks. :)
Jun 13 2011