Rogach
Rogach

Reputation: 27200

Compiler error when calling toArray

scala> def a[A](b:Seq[A]) = b.toArray
<console>:7: error: could not find implicit value 
                    for evidence parameter of type ClassManifest[A]
       def a[A](b:Seq[A]) = b.toArray
                              ^

What is the problem here? And how can I work around this?

Upvotes: 3

Views: 990

Answers (3)

Andy Petrella
Andy Petrella

Reputation: 4345

What you have to do is to specify the returnable type, this will work (for scala < 2.8):

def a[A](b:Seq[A]):Array[A] = b.toArray

Due to the new Collections framework which had to do special kind of conversion in order to handle Arrays like Collections see Fighting bit rot page 448, we have to tell about the high-order type, and it's the meaning of ClassManifest which tells about the class (there is a Manifest that is wider).

So both examples below are valid (more information here Collections API Explained):

 def a[A](b:Seq[A])(implicit m:ClassManifest[A]):Array[A] = b.toArray

 def a[A:ClassManifest](b:Seq[A]):Array[A] = b.toArray

Upvotes: 6

Paul Butcher
Paul Butcher

Reputation: 10852

You need to add a view bound to provide it with the manifest:

def a[A: ClassManifest](b:Seq[A]) = b.toArray

Upvotes: 3

missingfaktor
missingfaktor

Reputation: 92046

scala> def a[A : ClassManifest](b:Seq[A]) = b.toArray
a: [A](b: Seq[A])(implicit evidence$1: ClassManifest[A])Array[A]

scala>

Upvotes: 4

Related Questions