D's type classes pattern ?

matovitch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 24 08:57:40 PDT 2015


More like :

import std.stdio;

interface IStuff(Stuff)
{
      void foo();
}

class TypeClass(T, I) : I(T)
{
      alias this stuff;
      T stuff;
}

void myAwesomeAlgo(Stuff) (TypeClass!(Stuff, IStuff) stuff)
{
      stuff.foo();
}

struct MyStuff
{
     void foo()
     {
         writeln("Hello World !");
     }
}

void main()
{
     alias TypeStuff = TypeClass!(MyStuff, IStuff);
     TypeStuff stuff;
     myAwesomeAlgo(stuff);
}

Doesn't compile btw :

kmeans.d(8): Error: members expected
kmeans.d(8): Error: { } expected following aggregate declaration
kmeans.d(8): Error: Declaration expected, not '('
kmeans.d(12): Error: unrecognized declaration


More information about the Digitalmars-d-learn mailing list