I have some difficulties to understand when use and when not use typeclass in my code. I mean create my own, and not use already defined typeclasses, of c
You can often use a data type instead of a type-class, e.g.
data Repairable a = Repairable
{ getRepairable :: a
, isRepairable :: Bool
, canBeRepairedWith :: [Tool] -> Bool -- just to give an example of a function
}
Of course you need to pass this value explicitly, but this can be a good thing if you have multiple choices (e.g. think of Sum
and Product
as possible Monoid
s for numbers). Except that you have more or less the same expressiveness as for a type-class.