ouertani
ouertani

Reputation: 347

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.

Upvotes: 1

Views: 162

Answers (1)

Didier Dupont
Didier Dupont

Reputation: 29528

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.

Upvotes: 1

Related Questions