【发布时间】:2016-05-03 03:02:04
【问题描述】:
我想要一个练习来创建某种幺半群,即。通用类型的自定义包装类,允许我调整它们的基本操作:
abstract class WrappedVal[T](value: T) {
def +(that: WrappedVal[T]): WrappedVal[T]
def get: T = value
}
case class NumericValue(value: Double) extends WrappedVal[Double](value) {
override def +(that: WrappedVal[Double]): NumericValue = NumericValue(value + that.get)
}
case class StringValue(value: String) extends WrappedVal[String](value) {
override def +(that: WrappedVal[String]): StringValue = StringValue(value.substring(1) + that.get.substring(1))
}
这样,我可以例如做NumericValue(3)+NumericValue(4),然后我得到NumericValue(7)。
然后我想把这个值多包装一点,所以我补充说:
case class Entry(bucket: Integer, value: WrappedVal[_])
现在我有一个函数,它根据参数返回带有签名的条目类型(始终具有相同的值类型,即 NumericValue 或 StringValue 或 SomeOtherValue):
def readValue(vartype: String, value: String): WrappedVal[_] = {
if(vartype == "String") StringValue(value)
else NumericValue(value.toInt)
}
我很有效地得到:
val l = List(Entry(1,NumericValue(1)), Entry(1,NumericValue(2)), Entry(1,NumericValue(3)))
当我想使用自定义运算符时出现问题,例如在 reduce 子句中:
l.map(x => x.value).reduce(_+_)
发生这种情况是因为 scala 可以在运行时确定类型(l.map(x => x.value) 属于 List[WrappedVal[_]] 类型。
关于如何“以正确的方式”解决此问题的任何提示?
【问题讨论】:
-
我可以将 Entry 参数化为
Entry[T](bucket: Integer, value: WrappedVal[T]),但我只能在运行时告诉 readValue 的返回类型,这会导致所有混乱 - readValue 返回 WrappedValue[_]。
标签: scala types functional-programming