我有几个包含数值的类(该值可以有界,只能是正数,aso ...)
我必须根据超级类型'做一些基本的操作(比如求和)。那些课程。所以我为它定义了一个特点,认为它会变得微不足道......
以下是我的代码的可运行摘录:
object Main extends App {
trait WithValue[A] {
def value: A
}
class BoundedNumber[A](val lower: A, val upper: A, val value: A) extends WithValue[A]
case class NumberBetween0and99(value: Int) extends BoundedNumber[Int](0, 99, value)
case class UnboundedPositiveInt(value: Int) extends WithValue[Int]
case class UnboundedPositiveDouble(value: Double) extends WithValue[Double]
override def main(args: Array[String]) {
val map: Map[Symbol, WithValue[_]] = Map(
'foo -> UnboundedPositiveDouble(5),
'bar -> UnboundedPositiveInt(10),
'baz -> NumberBetween0and99(55)
)
for (m <- map) println(5 + m._2.value)
}
}
for循环失败:
overloaded method value + with alternatives: (...) cannot be applied to (Any)
我正在围着这个非常微不足道的问题圈了几个小时...猜测我在Scala的流利程度远远不够流利&#39; ...