3

我正在探索在 Scala 中抽象案例类的方法。例如,这是一个尝试Either[Int, String](使用 Scala 2.10.0-M1 和-Yvirtpatmat):

trait ApplyAndUnApply[T, R] extends Function1[T, R] {
  def unapply(r: R): Option[T]
}

trait Module {
  type EitherIntOrString
  type Left <: EitherIntOrString
  type Right <: EitherIntOrString
  val Left: ApplyAndUnApply[Int, Left]
  val Right: ApplyAndUnApply[String, Right]
}

鉴于这个定义,我可以写这样的东西:

def foo[M <: Module](m: M)(intOrString: m.EitherIntOrString): Unit = {
  intOrString match {
    case m.Left(i) => println("it's an int: "+i)
    case m.Right(s) => println("it's a string: "+s)
  }
}

这是模块的第一个实现,其中 的表示Either是 a String

object M1 extends Module {
  type EitherIntOrString = String
  type Left = String
  type Right = String
  object Left extends ApplyAndUnApply[Int, Left] {
    def apply(i: Int) = i.toString
    def unapply(l: Left) = try { Some(l.toInt) } catch { case e: NumberFormatException => None }
  }
  object Right extends ApplyAndUnApply[String, Right] {
    def apply(s: String) = s
    def unapply(r: Right) = try { r.toInt; None } catch { case e: NumberFormatException => Some(r) }
  }
}

unapplys 使LeftandRight真正排他性,所以以下工作符合预期:

scala> foo(M1)("42")
it's an int: 42

scala> foo(M1)("quarante-deux")
it's a string: quarante-deux

到目前为止,一切都很好。我的第二次尝试是scala.Either[Int, String]用作以下的自然实现Module.EitherIntOrString

object M2 extends Module {
  type EitherIntOrString = Either[Int, String]
  type Left = scala.Left[Int, String]
  type Right = scala.Right[Int, String]
  object Left extends ApplyAndUnApply[Int, Left] {
    def apply(i: Int) = scala.Left(i)
    def unapply(l: Left) = scala.Left.unapply(l)
  }
  object Right extends ApplyAndUnApply[String, Right] {
    def apply(s: String) = scala.Right(s)
    def unapply(r: Right) = scala.Right.unapply(r)
  }
}

但这不能按预期工作:

scala> foo(M2)(Left(42))
it's an int: 42

scala> foo(M2)(Right("quarante-deux"))
java.lang.ClassCastException: scala.Right cannot be cast to scala.Left

有没有办法得到正确的结果?

4

1 回答 1

1

问题出在这个匹配器中:

intOrString match {
    case m.Left(i) => println("it's an int: "+i)
    case m.Right(s) => println("it's a string: "+s)
}

它无条件地m.Left.unapplyintOrString. 至于为什么会这样,请参见下文。

当你打电话时,foo(M2)(Right("quarante-deux"))这就是正在发生的事情:

  • m.Left.unapply解决M2.Left.unapply实际上是哪个scala.Left.unapply
  • intOrStringRight("quarante-deux")

因此,scala.Left.unapply被调用Right("quarante-deux")导致CCE。

现在,为什么会发生这种情况。当我尝试通过解释器运行您的代码时,我收到了以下警告:

<console>:21: warning: abstract type m.Left in type pattern m.Left is unchecked since it is eliminated by erasure
           case m.Left(i) => println("it's an int: "+i)
                  ^
<console>:22: warning: abstract type m.Right in type pattern m.Right is unchecked since it is eliminated by erasure
           case m.Right(s) => println("it's a string: "+s)
                   ^

unapply方法ApplyAndUnApply被擦除为Option unapply(Object)。因为不可能运行类似的东西intOrString instanceof m.Left(因为m.Left也被擦除了),所以编译器编译这个匹配来运行所有被擦除unapply的 s。

获得正确结果的一种方法如下(不确定它是否符合您最初抽象案例类的想法):

trait Module {
    type EitherIntOrString
    type Left <: EitherIntOrString
    type Right <: EitherIntOrString
    val L: ApplyAndUnApply[Int, EitherIntOrString]
    val R: ApplyAndUnApply[String, EitherIntOrString]
}

object M2 extends Module {
    type EitherIntOrString = Either[Int, String]
    type Left = scala.Left[Int, String]
    type Right = scala.Right[Int, String]
    object L extends ApplyAndUnApply[Int, EitherIntOrString] {
        def apply(i: Int) = Left(i)
        def unapply(l: EitherIntOrString) = if (l.isLeft) Left.unapply(l.asInstanceOf[Left]) else None
    }
    object R extends ApplyAndUnApply[String, EitherIntOrString] {
        def apply(s: String) = Right(s)
        def unapply(r: EitherIntOrString) = if (r.isRight) Right.unapply(r.asInstanceOf[Right]) else None
    }
}
于 2012-03-03T02:45:53.633 回答