javax.persistence.PersistenceException: No Persistence provider for EntityManager named dsg-jpa
Now, without diving into the 'why' of this annoyance, I changed over to my handy Groovy shell (NOTE: I almost always ship both a Groovy and a Scala console in my applications so that I can use either as needed). Voila, that annoying exception disappeared. However, now I found myself working in Groovy-land but calling code that returned Scala collections. The good thing is that the semantics working with Groovy and Scala collections are similar:
// --- Groovy // each(grooy.lang.Closure) javaList.each { item -> println(item) } // --- Scala // foreach(scala.Function1) scalaList.foreach { item => println(item) }
However, on the one hand, being a Scala collection and not a java collection there's no each method to be called. If you try to use it in a Groovy fashion you'll get the following:
// In Groovy Shell scalaList.each { item -> println(item) }
ERROR java.lang.ClassCastException: scala.collection.JavaConversions$JIteratorWrapper cannot be cast to java.util.Iterator
On the other hand, if you call it using Scala's foreach you'll get the following:
// In Groovy Shell scalaList.foreach { item -> println(item) }
ERROR groovy.lang.MissingMethodException: No signature of method: scala.collection.JavaConversions$JListWrapper.foreach() is applicable for argument types: (groovysh_evaluate$_run_closure1) values: [groovysh_evaluate$_run_closure1@46aea8cf] Possible solutions: each(groovy.lang.Closure) // Not very helpful is it
Fortunately, Groovy allows us to very easily cast a groovy.lang.Closure to a Scala function, in this case a scala.Function1. So the following will work:
// In Groovy Shell scalaList.foreach({ item -> println(item) } as scala.Function1)
0 comments:
Post a Comment