Why aren't there many discussions about co- and contra-variance in Haskell (as opposed to Scala or C#)?

后端 未结 3 1468
别跟我提以往
别跟我提以往 2020-12-13 05:49

I know what covariance and contravariance of types are. My question is why haven\'t I encountered discussion of these concepts yet in my study of Haskell (as opposed to, say

3条回答
  •  情歌与酒
    2020-12-13 06:32

    There are no "sub-types" in Haskell, so covariance and contravariance don't make any sense.

    In Scala, you have e.g. Option[+A] with the subclasses Some[+A] and None. You have to provide the covariance annotations + to say that an Option[Foo] is an Option[Bar] if Foo extends Bar. Because of the presence of sub-types, this is necessary.

    In Haskell, there are no sub-types. The equivalent of Option in Haskell, called Maybe, has this definition:

    data Maybe a = Nothing | Just a
    

    The type variable a can only ever be one type, so no further information about it is necessary.

提交回复
热议问题