Should I use typeclasses or not?

后端 未结 2 1285
傲寒
傲寒 2020-12-05 10:35

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

2条回答
  •  [愿得一人]
    2020-12-05 11:04

    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 Monoids for numbers). Except that you have more or less the same expressiveness as for a type-class.

提交回复
热议问题