DEV Community

Discussion on: Objects as Primitives?

Collapse
 
socratesdz profile image
Sócrates Díaz

I'm no Haskell expert. But Haskell typeclasses allow you to do that.

To understand the basics, look at typeclasses like C# or Java interfaces, they define one or more methods that your instances should implement.

Eq is a typeclass that allows you to evaluate a value as a boolean. Show is a typeclass that represents a value as a readable String.

Then assuming you have the following data type:

data Animal = Animal String

You can also write the following:

instance Eq Animal where
    (Animal name) == (Animal name) = name == name

instance Show Animal where
    show (Animal name) = name

And that way you can use a data type Animal for equality or as a string. To treat it like a boolean value, there's another typeclass called BoolValue that can do that.