Converting from java.util.ArrayList to scala.collection.immutable.List, the 2.10 compiler and run-time may seem to be in disagreement, about the type of val emits:
import org.ahocorasick.trie._ import scala.collection.JavaConverters._ // convert Java colllections to Scala ones object wierd { val trie = new Trie def trieInit(patterns: List[String]) { trie.onlyWholeWords(); for (pattern <- patterns) trie.addKeyword(pattern) } def patternTest(text : String) : List[String] = { val emitsJ = trie.parseText(text) val emits = emitsJ.asScala map (i => i.getKeyword) println(s"converted from ${emitsJ.getClass} to ${emits.getClass}") //return(emits) return (List.empty[String]) } trieInit(List("hello")) patternTest("hello") } Yields:
converted from class java.util.ArrayList to class scala.collection.immutable.$colon$colon
Now changing to return the real value by changing only the return line -
import org.ahocorasick.trie._ import scala.collection.JavaConverters._ // convert Java colllections to Scala ones object wierd { val trie = new Trie def trieInit(patterns: List[String]) { trie.onlyWholeWords(); for (pattern <- patterns) trie.addKeyword(pattern) } def patternTest(text : String) : List[String] = { val emitsJ = trie.parseText(text) val emits = emitsJ.asScala map (i => i.getKeyword) println(s"converted from ${emitsJ.getClass} to ${emits.getClass}") return(emits) //return (List.empty[String]) } trieInit(List("hello")) patternTest("hello") } Yields a compilation error:
[error] reproduce.scala:23: type mismatch; [error] found : Iterable[String] [error] required: List[String] [error] return(emits) [error] ^ [error] one error found [error] (compile:compile) Compilation failed What would be the simple explanation for this? How would I better approach the conversion?