2012-02-12 71 views
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表示是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) } 
    } 
} 

unapply个奇妆的LeftRight真正独家的,所以下面的作品期待:

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 

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

回答

1

问题是此匹配:

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

无条件地对intOrString执行m.Left.unapply。至于它的原因,请参阅下文。

当你调用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) 
       ^

ApplyAndUnApplyunapply方法被擦除Option unapply(Object)。由于不可能运行类似intOrString instanceof m.Left(因为m.Left也被擦除),所以编译器编译该匹配以运行所有擦除的unapplys。

一种方式得到正确的结果低于(如果它与你原来的抽象case类的想法一起去不知道):

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 
    } 
} 
+0

很好的解释!我决定通过不使用模式匹配来解决我的问题:-)定义折叠函数更容易,更强大。 – betehess 2012-03-03 22:13:27