On Sun, Mar 18, 2012 at 02:23, Haisheng Wu <freizl@gmail.com> wrote:
Hi there,
 Do you have any idea why the following code is not valid?

class Guess g where
 makeGuess :: g -> String

instance Guess GuessLetter where
 makeGuess = ...

instance Guess GuessWord where
 makeGuess = ...

-- | This function is nod valid
nextGuess :: Guess g => String -> g
nextGuess = ...


You are probably assuming that you get to decide within nextGuess what type it will return; in fact, the type it will return is set by its context when invoked, and your implementation must be prepared to return any Guess based on what the caller wants.  Since nextGuess has no way to *get* at that context, and the typeclass has no method that can be used to produce a result based on the requested result type (as with e.g. Read), it's not clear to me that it can actually return anything (that is, the only possible result is _|_).

Mantra:  "Typeclasses are not OOP."  Don't try to think of them as OOP, you will only get into trouble.

--
brandon s allbery                                      allbery.b@gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms