【发布时间】:2016-02-22 04:07:08
【问题描述】:
当使用抽象类型成员时,如果不使用 asInstanceOf,我就无法从外部使用该类型,还有其他选择吗?为什么抽象类型不让自己被“覆盖”以便知道它是 Int 还是 String?
scala> trait Param { type A
| val x:A
| def get:A = x
| }
scala> case class IParam(override val x:Int) extends Param {type A = Int}
defined class IParam
scala> case class SParam(override val x:String) extends Param {type A = String}
defined class SParam
scala> val s = Set[Param](IParam(1), SParam("s"))
s: scala.collection.immutable.Set[Param] = Set(IParam(1), SParam(s))
scala> val y:Int = s.head.get
<console>:26: error: type mismatch;
found : Param#A
required: Int
val y:Int = s.head.get
【问题讨论】:
标签: scala