scala type woes

You can sometimes encounter warnings like:

not enough arguments for method toArray: (implicit evidence$1: ClassManifest[B])Array[B].
[error] Unspecified value parameter evidence$1.
[error] lazy val randomKeys = shuffle(myCollection.toArray)
[error] ^

in relation to code like this:

def shuffle[T](v : Array[T]) : Array[T] = {...}
shuffle(myCollection.toArray)

The error is a bit misleading here – what the compiler is actually trying to say is that it doesn’t have enough information to figure out which version of the shuffle method to dispatch, so you have to help it:

shuffle[MyCollection](myCollection.toArray)

To the best of my understanding, the reason the compiler can’t infer the proper types in this case is due to how the JVM handles parameterized types (via erasure). The explicit type annotation isn’t necessary, for instance if shuffle is defined to take a Scala List[T]:

def shuffle[T](v : List[T]) : List[T] = {...}
shuffle(myCollection.toList)

In this case, I’m assuming that enough information can be carried through via ClassManifest to determine what needs to happen.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s