简体   繁体   English

为什么scalaz Semigroup不协变?

[英]Why scalaz Semigroup is not covariant?

Is their a simple raison why Scalaz SemiGroup is not covariant : 为什么Scalaz SemiGroup不协变是他们的简单理由?

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/Semigroup.scala

https://github.com/scalaz/scalaz/blob/series/7.1.x/core/src/main/scala/scalaz/syntax/SemigroupSyntax.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 类型参数在semigroup的main操作append中同时出现在协变和协变位置(结果和自变量),因此它既不能协变也不能协变

Just to give a simple counter example, consider Seq[Int] ( Int is just to fix the type, could be anything). 仅举一个简单的反例,考虑Seq[Int]Int只是用来修复类型,可以是任何东西)。 You can easily define a semigroup there, with append being ++ . 您可以轻松地在其中定义一个半群,其append++ 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. 现在Option不扩展Seq,但很容易安排一个类似于选项的类型来扩展Seq(甚至只是类型为case class Single[A](a: A) extends Seq[A] )。 Seq[Int]Seq[Int]不能是Option [Int]或Single [Int]的半组,附加两个选项不会给出一个选项,也不会附加两个单值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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