Re: D's type classes pattern ?

2015-03-25 Thread matovitch via Digitalmars-d-learn
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

Re: D's type classes pattern ?

2015-03-25 Thread bearophile via Digitalmars-d-learn
matovitch: 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. Take a look at the sources and

Re: D's type classes pattern ?

2015-03-25 Thread matovitch via Digitalmars-d-learn
On Wednesday, 25 March 2015 at 08:55:14 UTC, bearophile wrote: matovitch: 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

Re: D's type classes pattern ?

2015-03-24 Thread anonymous via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 16:56:13 UTC, matovitch wrote: Thanks, just to be clear : void Bar(T : Foo)(T t){ } is the same as void Bar(T)(T t) if (is(T == Foo)){ } and it is checked only at compile time ? (for the runtime I know that what interface were meant for ;)). Ali already

Re: D's type classes pattern ?

2015-03-24 Thread Ali Çehreli via Digitalmars-d-learn
On 03/24/2015 08:50 AM, matovitch wrote: Lets say I want to implement some generic algorithm. I would like to checks the types passed to my algorithm implements a specific interface. I think you are looking for template constraints. Look at isInputRange's implementation:

Re: D's type classes pattern ?

2015-03-24 Thread Ali Çehreli via Digitalmars-d-learn
On 03/24/2015 09:56 AM, matovitch wrote: just to be clear : void Bar(T : Foo)(T t){ } That means if T can implicitly be converted to Foo. is the same as void Bar(T)(T t) if (is(T == Foo)){ } That means if T is exactly Foo. and it is checked only at compile time ? Yes to both. Ali

D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Hi, It's been a long time since I coded some d code... sorry I take the lazy way asking for advices. :D Lets say I want to implement some generic algorithm. I would like to checks the types passed to my algorithm implements a specific interface. interface IStuff(Stuff) { void foo(); }

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
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

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Well, just follow that link to the code...it almost compile : http://dpaste.com/3JNP0QD.

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
Wait no ! In that case my type will have to inherit the interface...I don't want that, checking without inheriting...I know thats weird.

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 16:44:54 UTC, weaselcat wrote: On Tuesday, 24 March 2015 at 15:51:00 UTC, matovitch wrote: Hi, It's been a long time since I coded some d code... sorry I take the lazy way asking for advices. :D Lets say I want to implement some generic algorithm. I would like to

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
To resume my goal (last lonely message I promise), how can you statically check a type implement an interface without making this type inherit the interface (otherwise std.traits would do it) ? ps : it seems to me that this is exactly what the haskell compiler do with type classes - layman

Re: D's type classes pattern ?

2015-03-24 Thread matovitch via Digitalmars-d-learn
well, alias this is the issue here. interface I { void foo(); } struct S { void foo() {} } class C : I { S s; alias this s; } don't compile...if you have any idea to do otherwise I am greatly interested. Thanks !

Re: D's type classes pattern ?

2015-03-24 Thread weaselcat via Digitalmars-d-learn
On Tuesday, 24 March 2015 at 15:51:00 UTC, matovitch wrote: Hi, It's been a long time since I coded some d code... sorry I take the lazy way asking for advices. :D Lets say I want to implement some generic algorithm. I would like to checks the types passed to my algorithm implements a