I have a function that returns ( groovy code)
[words: "one two", row: 23, col: 45]
In scala I change above to scala Map but then I am forced to declare it as
Map[String, Any]
but this has the disadvantage that if I access a key such as
map(“words”)
I have to add the boilerplate
map2("words").asInstanceOf[String]
Is there a better way in scala that does not require me to add asInstanceOf?
In order to avoid the casting and benefit from static-typing, you can either return a tuple
(String, Int, Int):or use a case class for the result:
I would prefer the case class because the fields are named and it’s really just one line more.