cannot find class manifest for element type T
Asked Answered
S

1

16

Was trying to compile some code from this SO question and run into this error message cannot find class manifest for element type T. Here is another snippet that shows the behavior:

scala> def f[T](a:T, b:T):Array[T] = { new Array[T](2) }
<console>:4: error: cannot find class manifest for element type T
       def f[T](a:T, b:T):Array[T] = { new Array[T](2) }

I can see that new collection.mutable.GenericArray[T](2) fixes the issue. Apparently providing a manifest is the other option... But what does "providing a manifest mean"?

Seriocomic answered 12/2, 2010 at 14:58 Comment(1)
Note that GenericArray was renamed to ArraySeq in Scala 2.8 final.Catchfly
C
19

To provide type information you can use a context bound

def f[T : Manifest](a:T, b:T):Array[T] = { new Array[T](2) }

or the manifest as an implicit argument:

def f[T](a:T, b:T)(implicit manifest : Manifest[T]) : Array[T] = { new Array[T](2) }

The former is syntactic sugar for the later. The manifest is needed because the type information about T is missing due to generic type errasure of the JVM.

Calendre answered 12/2, 2010 at 15:22 Comment(3)
Some information is also given by Martin in this paper scala-lang.org/sites/default/files/sids/cunei/…Pappose
See also #3214010 for more on manifests and type erasure.Catchfly
AFAIK ClassManifest is sufficient in this case.Zitvaa

© 2022 - 2024 — McMap. All rights reserved.