I want to call one function (or the same but with a special argument, no matter) if a type instantiates some type-class or another - if not. I know that it's easy in languages like D, Java, possible C++ - we can specialize templates more generic or less generic and to restrict its parameters with class/interface implementation/extending (there are special keywords/type operators for it, used in generics/templates signatures). So, I suppose Haskell can do the same. But as it turned out, this is not so easy for Haskell :(
I found these useful links:
So, I wrote something like this (this example is compiling and works as I expect, you need `mustache` and `network-uri` packages). Goal is to substitute type in special way for HTML template and for text template if type implements a special interface (for deterministic substitution). Otherwise default substitution will be used. Actually, this code allows usage of not only HTML/text templates... OK, here it is:
I don't know am I understand correctly the Haskell solution but this looks close to explained in the Haskell Wiki (case 1). But in all cases I see problem: always I need not only to implement this special interface but to "say" that a type implements special (deterministic) substitution, see:
This means that clients of this code must "implements" 2 instances: 1) of ToMustaches and 2) of SubPred (with DetSub as last type param). This is terrible and no such thing in other languages! So, my question is: is a simple way to avoid it?
Best regards, Paul
Beginners mailing list
|Free forum by Nabble||Edit this page|