简体   繁体   English

协变类型E出现在类型A => returnerror.Error [E,B]值f的相反位置

[英]covariant type E occurs in contravariant position in type A => returnerror.Error[E,B] of value f

I have this definition: 我有这个定义:

sealed trait Error[+E, +A] {
  final def map[B](f: A => B): Error[E, B] = {
    this match {
      case Ok(a) => Ok(f(a))
      case Fail(e) => Fail(e)
    }
  }
  final def flatMap[B](f: A => Error[E, B]): Error[E, B] = {
    this match {
      case Ok(a) => f(a)
      case Fail(e) => Fail(e)
    }
  }
}

object Error {
  final case class Ok[A](a: A) extends Error[Nothing, A]
  final case class Fail[E](e: E) extends Error[E, Nothing]
}

This won't compile with this error: 这不会与以下错误一起编译:

Error.scala:12: covariant type E occurs in contravariant position in type A => returnerror.Error[E,B] of value f
  final def flatMap[B](f: A => Error[E, B]): Error[E, B] = {
                       ^
one error found

I don't get why. 我不明白为什么。 Can you, please, explain why this doesn't compile? 您能否解释一下为什么不编译? Thanks 谢谢

I will simplify the example a bit in order to make it easier to read. 为了简化阅读,我将稍微简化一下示例。

Imagine you have trait like this and let's assume that such declaration is legal: 假设您具有这样的trait并假设这样的声明是合法的:

trait Error[+E]  {
  def someMethod (f: String => Error[E])  // actually illegal
}

And implementations 和实现

class ErrorString extends  Error[String]  {

  def someMethod (f: String => Error[String]) = {
    val someVar: Error[String] = f("Somestring")
  }

}

class ErrorInt extends  Error[Int]  {
    ...
} 

As E is covariant we can treat Error[String] as subtype of Error[Any] , so we can write the follwing 由于E是协变的,我们可以将Error[String]视为Error[Any]子类型,因此我们可以写下面的代码

val anyError: Error[Any] = new ErrorString()

And then pass String => Error[Int] function as a parameter to someMethod 然后将String => Error[Int]函数作为参数传递给someMethod

val intErrorReturnFunction: String => Error[Any] = (k) => new ErrorInt
anyError.someMethod(intErrorReturnFunction) // actually illegal

But as anyError is still have type Error[String] in a background means that we are trying to pass String => Error[Int] function to method with String => Error[String] parameter. 但是由于anyError在后台仍然具有类型Error[String] ,这意味着我们试图将String => Error[Int]函数传递给具有String => Error[String]参数的方法。

So at the end we would have something like this 所以最后我们会有这样的事情

 val someVar: Error[String] = f("Somestring"):Error[Int]

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

相关问题 协变类型 A 出现在值 a 的类型 A 中的逆变位置 - covariant type A occurs in contravariant position in type A of value a 错误:协变类型A出现在逆变位置 - Error: Covariant type A occurs in contravariant position 协变类型 T 出现在逆变位置 - covariant type T occurs in contravariant position 为什么Scala编译器说逆向类型A出现在类型>中的协变位置:A <:任何类型B? - Why does Scala compiler say that contravariant type A occurs in covariant position in type >: A <: Any of type B? 协变类型 FParam 出现在值猜测的类型 Seq[FParam] 中的逆变位置 - Covariant type FParam occurs in contravariant position in type Seq[FParam] of value guesses 在函数参数 A =&gt; B 的逆变位置接受协变类型 A - Covariant type A accepted in contravariant position of function argument A => B val使用中的无效方差-错误:协变类型U发生在协变位置 - Invalid Variance in val use - error: contravariant type U occurs in covariant position 在协变位置上使用协变类型是否有其他选择? - Is there any alternatives to using covariant type in contravariant position? 错误:协变类型C发生在类型的不变位置? - Error: covariant type C occurs in invariant position in type? 如何在协变位置上使用协变类型参数来解决 - How to workaround using a covariant type parameter in a contravariant position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM