Typeclasses and overloading, what is the connection?

后端 未结 4 2050
[愿得一人]
[愿得一人] 2020-12-24 14:31

I am currently trying to wrap my head around typeclasses and instances and I don\'t quite understand the point of them yet. I have two questions on the matter so far:

<
4条回答
  •  無奈伤痛
    2020-12-24 14:52

    This only answers question 1 (directly, at least).

    The type signature f :: a -> a -> Bool is shorthand for f :: forall a. a -> a -> Bool. f would not truly work for all types a if it only works for as that have (==) defined. This restriction to types that have (==) is expressed using the constraint (Eq a) in f :: forall a. (Eq a) => a -> a -> Bool.

    "For all"/universal quantification is at the core of Haskell's (parametric) polymorphism and, among other things, provides the powerful and important properties of parametricity.

提交回复
热议问题