Scala: Store parameter names and types in a hash map
scala, types
Solution
`Class` is generic, and Scala isn't as cavalier as Java about allowing you to ignore facts like that. The following will work just fine:
val attribTemplate = new LinkedHashMap[String, Class[_]]
attribTemplate("attr1") = classOf[Int]
attribTemplate("attr2") = classOf[String]
And then:
scala> println(attribTemplate)
Map(attr1 -> int, attr2 -> class java.lang.String)
This is almost certainly a bad idea, though, and it's not idiomatic Scala. Especially if you're new to Scala, I'd suggest asking another question about your particular use case and avoiding reflection (and mutability, if you can) for as long as possible.
Problem
I am relatively new to scala. I am trying to maintain a hash map containing list of attributes and their types in a hash map. I tried something like this ``` val attribTemplate = new mutable.LinkedHashMap[String, Class] attribTemplate("attr1") = classOf[Int] attribTemplate("attr2") = classOf[String] ``` scala doesn't like it. I would like to do pattern matching on this type information later How can I achieve this? Thanks