How to create classes in Haskell?

Trying to create a base class from which I can get different types. What happened to the following?

class (Eq a) => MyClass a 

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

I get an error message:

test.hs:5:10: `==' is not a (visible) method of class `MyClass'
Failed, modules loaded: none.
+3
source share
3 answers

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

data Alpha = Alpha
instance Eq Alpha where
    Alpha == Alpha = True
instance MyClass Alpha
+8
source

The first line says that you need to declare Alpha an Eq instance first, and then MyClass.

+2
source

, , Haskell , - . Typeclasses Java.

. - - , . Java , .

+1
source

Source: https://habr.com/ru/post/1733608/


All Articles