About Scala generics: cannot find class manifest for element type T

Simply add a context bound ClassManifest to your method declaration:

def reverse[T : ClassManifest](a: Array[T]): Array[T] = ...

In order to construct an array, the array's concrete type must be known at compile time. This type is supplied by the compiler via an implicit ClassManifest parameter. That is, the signature of the Array constructor is actually

Array[T](size: Int)(implicit m: ClassManifest[T]): Array[T]

In order to supply this parameter, there must be a ClassManifest in scope when the Array constructor is invoked. Therefore, your reverse method must also take an implicit ClassManifest parameter:

def reverse[T](a: Array[T])(implicit m: ClassManifest[T]): Array[T] = ...
// or equivalently
def reverse[T : ClassManifest](a: Array[T]): Array[T] = ...

The latter, simpler notation is called a context bound.


When using [T : ClassManifest] if it is shown as deprecated use [T : ClassTag]

Tags:

Generics

Scala