D's type classes pattern ?
matovitch via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Mar 25 01:16:25 PDT 2015
Thanks for the precisions on template constraint and template
specialization...Indeed wath I want to do look like isInputRange
constraint. Haskell have something like :
//(Pseudo D-Haskell)
void foo(InputRange R)(R r);
//D equivalent
void foo(R)(R r) if (isInputRange(R));
Except they call them type classes instead of template constraint
and it is check at runtime instead of compile time for D.
I am curious to know how isInputRange is implemented since I
wanted to do kind of the same but I am afraid it's full of (ugly)
traits and template trickeries where haskell type classes are
quite neat and essentially a declaration of an interface.
Let say I want to be able to add the type my algo deal with...I
could do an isAddable wich looks if a+b compiles with traits...I
wondered if you could check statically that the type could
implement an interface *if it wanted to* that is, without
inheriting it...
More information about the Digitalmars-d-learn
mailing list