更新时间:2023-09-15 23:24:40
那是因为 testArray
中 T
的具体类型在编译时是未知的.您的签名必须看起来像 def testArray[T : ClassManifest](n: Int, gen: =>T)
,这将添加类型为 ClassManifest[T]
的隐式参数code> 到您的方法,它会自动传递给 testArray
的调用,然后进一步传递给 Array.fill
调用.这称为上下文绑定
.
That is because in testArray
the concrete type of T
is not known at compile time. Your signature has to look like def testArray[T : ClassManifest](n: Int, gen: =>T)
, this will add an implicit parameter of type ClassManifest[T]
to your method, that is automatically passed to the call of testArray
and then further passed to the Array.fill
call. This is called a context bound
.