
Hi, I am bit struggling with understanding the typeclasses usage in Text.Regex.Posix (=~) while reading Real world haskell. The type of (=~) is (RegexMaker Regex CompOption ExecOption source, RegexContext Regex source1 target) => source1 -> source -> target I am from Java world. My question is that can I understand source1 in the way which is a value has type of RegexMaker, Regex, CompOption and ExecOption? And the definiton of instance RegexMaker Regex CompOption ExecOption Stringmakes it possible for us to pass a String as the parameter of =~? Where can I find some good metarials about GHC's type classes? I googled a lot but can not find something mentioned above usage. -- Cheers, Keke ----------------- We paranoid love life

I am bit struggling with understanding the typeclasses usage in Text.Regex.Posix (=~) [...] My question is that can I understand source1 in the way which is a value has type of RegexMaker, Regex, CompOption and ExecOption?
Prettifying the signature a little bit: :: ( RegexMaker Regex CompOption ExecOption source , RegexContext Regex source1 target ) => source1 -> source -> target You can read this as: The type `source1 -> source -> target` such that both "class constraints" are satisfied. What is a class contraint and what does it mean for it to be satisfied? A class contraint is something like an interface, or a generic, or a template. One defines "instances" of classes in Haskell, as in Jave one "implements" an interface. So what do these particular classes mean? The first one reads: We have a way to make a regular expression using the usual option specifications (multiline, case insensitive, stuff like that -- those are `CompOption` and `ExecOption`). The second one reads There is a way to match the first value against the second to get the desired target value (list of matches, Boolean, &c.). The "way to..." in each case is a function in the class definition -- `makeRegex` in the former case, and `match` in the latter. -- Jason Dusek
participants (2)
-
Jason Dusek
-
keke