BLS wrote: > On 29/09/2010 22:28, Jonathan M Davis wrote: >> Look in >> std.range for the whole list, but isRandomAccesRange() handles random >> access >> ranges. Since there is no interface for ranges, you can't check >> generically >> whether a particular type correctly implements a particular range type >> - e.g >> is(mystruct : RandomAccessRange) or implements!(RandomAccessRange, >> mystrucT). > > > Thanks ! This is already something. (not exactly what I am > looking/hoping for) > > why we don't have /struct A implements IA {}/ by the way.
Lots of people have already answered why you can't have a struct "inheriting" from an interface, but there is nothing that prevents you from having a struct which includes all the functions defined by the interface. Given the capabilities of D for compile-time introspection, it should even be possible to write implements!(Interface, Struct) by having it enumerating the functions of the interface and checking for their presence in the struct. Kind of a duck-typing where the "duck" is defined by an interface: interface IFoo { void foo(); } struct SFoo { void foo() { writeln ("SFoo.foo"); } } template TFoo (T) if structImplements!(T, IFoo) { IFoo convert (T value) { return (IFoo)value; // Wrong because T does not inherit // from IFoo } void foo (T value) { value.foo(); // Right because T duck-implements IFoo } } This probably won't bring much above and over what would happen without the template constraint (after all, the compiler will already check that "value.foo()" is valid), but it can bring a lot in terms of documentation. Jerome -- mailto:jeber...@free.fr http://jeberger.free.fr Jabber: jeber...@jabber.fr
signature.asc
Description: OpenPGP digital signature