Scala中奇怪的类型不匹配错误

时间:2016-05-15 08:44:04

标签: scala

scala-user已经问过,没有得到答案。

我希望以下编译:

trait Elems {

  trait Dummy

  abstract class Elem[A] extends Serializable with Dummy

  class BaseElem[A] extends Elem[A]

  implicit val BooleanElement: Elem[Boolean] = new BaseElem[Boolean]
  implicit val ByteElement: Elem[Byte] = new BaseElem[Byte]
  implicit val ShortElement: Elem[Short] = new BaseElem[Short]
  implicit val IntElement: Elem[Int] = new BaseElem[Int]
  implicit val LongElement: Elem[Long] = new BaseElem[Long]
  implicit val FloatElement: Elem[Float] = new BaseElem[Float]
  implicit val DoubleElement: Elem[Double] = new BaseElem[Double]
  implicit val UnitElement: Elem[Unit] = new BaseElem[Unit]
  implicit val StringElement: Elem[String] = new BaseElem[String]
  implicit val CharElement: Elem[Char] = new BaseElem[Char]
}

trait GoodMatch { self: Elems =>

  private def boxed_class(e: Elem[_]): Class[_] = e match {
    case BooleanElement => classOf[java.lang.Boolean]
    case ByteElement => classOf[java.lang.Byte]
    case ShortElement => classOf[java.lang.Short]
    case IntElement => classOf[java.lang.Integer]
    case LongElement => classOf[java.lang.Long]
    case FloatElement => classOf[java.lang.Float]
    case DoubleElement => classOf[java.lang.Double]
    case CharElement => classOf[java.lang.Character]
    case _ => ???
  }

}

abstract class BadMatch[+A <: Elems](scalan: A) {
  import scalan._

  protected def toLuaValue(x: Any, eX: Elem[_]): String = eX match {
    case UnitElement => ""
    case _ => ???
  }

  // should check type before conversion?
  protected def fromLuaValue[B](lv: Any, eA: Elem[B]): B = (eA match {
    case UnitElement => ()
  }).asInstanceOf[B]

}

GoodMatch确实如此,但BadMatch失败(在Scala 2.11.8中):

[error] /tmp/rendererqu0xjasKpX/src/main/scala/test.scala:48: type mismatch;
[error]  found   : BadMatch.this.scalan.Elem[Unit]
[error]  required: BadMatch.this.scalan.Elem[_$3] where type _$3
[error]     case UnitElement => ""
[error]          ^
[error] /tmp/rendererqu0xjasKpX/src/main/scala/test.scala:63: type mismatch;
[error]  found   : BadMatch.this.scalan.Elem[Unit]
[error]  required: BadMatch.this.scalan.Elem[B]
[error]     case UnitElement => ()
[error]          ^

删除with Dummy也会使BadMatch编译。

这是Scala错误吗?如果是这样,它是一个已知的吗?

1 个答案:

答案 0 :(得分:1)

是的,它是Scala编译器错误:https://issues.scala-lang.org/browse/SI-9779