views:

306

answers:

2

I'm trying to use a JDO with Google App Engine and Scala. The api for the execute returns Object (but it's really a java collection) and I want to get it into a scala list to iterate over it.

My code looks like this so far:

val pm = PMF.factory.getPersistenceManager
val query = "select from User " 
val gamelist:List[User] = List(pm.newQuery(query).execute.toArray:_ *)

The compile error at this point is toArray is not a member of Object. What is the best way to do the above? I tried to use .asInstanceOf[java.util.Collection[User], but it was a failed attempt.

+1  A: 

The problem is that the Java collection is not a scala collection. Youy need the implicit conversions in the jcl package:

import collections.jcl.Conversions._
import java.util.{Collection => JCollection}

val pm = PMF.factory.getPersistenceManager
val query = "select from User " 

val users = pm.newQuery(query).execute.asInstanceOf[JCollection[User]]
val gamelist:List[User] = List(users.toArray: _*) //implicit conversion here
oxbow_lakes
+1  A: 

Use scala.collection.jcl.Conversions:

import scala.collection.jcl.Conversions._
...
// this gets you a List[User]
val gameList = pm.newQuery(query).execute.asInstanceOf[java.util.List[User]].toList
...
// or you can just iterate through the return value without converting it to List
pm.newQuery(query).execute.asInstanceOf[java.util.List[User]] foreach (println(_))
Walter Chang