Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly instantiate classes in Haskell?

Trying to create a base class from which I can derive different types. What's wrong with the following?

class (Eq a) => MyClass a 

data Alpha = Alpha
instance MyClass Alpha where
    Alpha == Alpha = True

I get the error:

test.hs:5:10: `==' is not a (visible) method of class `MyClass'
Failed, modules loaded: none.
like image 638
me2 Avatar asked Dec 22 '22 05:12

me2


1 Answers

You have to make Alpha an instance of Eq explicitly. This will work:

data Alpha = Alpha
instance Eq Alpha where
    Alpha == Alpha = True
instance MyClass Alpha
like image 85
sepp2k Avatar answered Dec 29 '22 19:12

sepp2k