where to find Implements!

"Jérôme M. Berger" jeberger at free.fr
Thu Sep 30 14:46:20 PDT 2010


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:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100930/0ca65d9f/attachment.pgp>


More information about the Digitalmars-d mailing list