我尝试编写一些 Scala 类
abstract class A { var a : Int = _}
class B[T] extends A { var b : T = _ }
class C[T] extends A { var c : T = _ }
class Abc[T : Manifest] {
var array : Array[T] = _
def this(capacity : Int, f : Unit => T) = {
this()
array = new Array[T](capacity)
for(i <- 0 until capacity)
array(i) = f()
}
}
class Xyz[T] {
var m : Abc[C[T]] = _;
def this(capacity : Int) = {
this();
m = new Abc[C[T]](capacity, Unit => { new C[T]() })
}
}
var xyz = new Xyz[Int](10)
但我得到了:
error: No Manifest available for C[T].
class Xyz[T] { var m : Abc[C[T]] = _; def this(capacity : Int) = { this(); m = new Abc[C[T]](capacity, Unit => { new C[T]() })}}
^
据我了解,我需要为 lambda 函数设置隐式 Manifest 参数
Unit => { new C[T]() })
但我怎样才能做到这一点呢?或者我完全错了?
您只需要从顶部一直携带清单,其中类型是已知的:
class Xyz[T : Manifest] { ...
应该这样做。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)