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错误吗?如果是这样,它是一个已知的吗?