简体   繁体   中英

Why scalaz Semigroup is not covariant?

Is their a simple raison why Scalaz SemiGroup is not covariant :

https://github.com/scalaz/scalaz/blob/series/7.1.x/core/src/main/scala/scalaz/Semigroup.scala

https://github.com/scalaz/scalaz/blob/series/7.1.x/core/src/main/scala/scalaz/syntax/SemigroupSyntax.scala

Thanks.

How could it be? the type parameter appears both in covariant and contravariant position (result and argument) in the main operation of semigroup, append , so it can be neither covariant nor contravariant

Just to give a simple counter example, consider Seq[Int] ( Int is just to fix the type, could be anything). You can easily define a semigroup there, with append being ++ . Now Option does not extends Seq, but it would be easy to arrange an option-like type that would extends Seq (or even just a type case class Single[A](a: A) extends Seq[A] ).Yet the semigroup of Seq[Int] could in no way be a semigroup of Option[Int] or Single[Int], appending two options does not give an option, nor appending two singles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM