我有一些问题将我的对象投射到Variable[A]
哪里A <: Array[_]
我创建了一个函数来比较manifest
数据并将其转换为 Array 到好的类型。
我的对象Variable[A]
将 a 存储Manifest[A]
到 def'type'
我制作了一个现有软件的插件,所以不是我Variable
用好的类型来实例化它。
原型对象和类:
object Prototype {
def apply[T](n: String)(implicit t: Manifest[T]) = new Prototype[T] {
val name = n
val `type` = t
}
}
trait Prototype[T] {
def name: String
def `type`: Manifest[T]
}
变量对象和类:
object Variable {
def apply[T](p: Prototype[T], v: T) = new Variable[T] {
val prototype = p
val value = v
}
}
trait Variable[T] {
def prototype: Prototype[T]
def value: T
}
我的班级使用:
class XYDataReader[A <: Array[_]](var data: Iterable[Variable[A]]) {
def get[T](variable: Variable[A])(implicit m: Manifest[T]): Option[T] = {
if (variable.prototype.`type` <:< m) {
Some(variable.value.asInstanceOf[T])
} else {
None
}
}
}
当我实例化用于比较的变量对象时,我可能有一个错误,所以我也给出了实例化的代码:
val v:List[Any] = List[Any](1.2,2,3)
val p = Prototype[Array[Any]]("col1")
val myVariable = Variable(p, v.toArray(ClassTag(p.`type`.runtimeClass)))
我不明白为什么当我调用get[Array[Double]](myVariable)
where myVariable.value
contains an时模式匹配失败Array[Double]
当我println()
两个清单时:
- 变量数组类型:
Array[double]
- 米型:
Array[Double]
似乎Array[Double]
不是Array[double]
,我该如何解决/投射这个?