0

consider the following variables in scala :

val nestedCollection_1 = Array(
  "key_1" -> Map("key_11" -> "value_11"),
  "key_2" -> Map("key_22" -> "value_22"))

val nestedCollection_2 = Map(
  "key_3"-> ["key_33","value_33"],
  "key_4"-> ["key_44"->"value_44"])

Following are my questions :

1) I want to read the values of the variables nestedCollection_1, nestedCollection_2 and ensure that the value of the variables are of the format

Array[Map[String, Map[String, String]]

and

Map[String, Array[String]]]

2) Is it possible to get the detailed type of a variable in scala? i.e. nestedColelction_1.SOME_METHOD should return Array[Map[String, Map[String, String]] as type of its values

Łukasz
  • 8,555
  • 2
  • 28
  • 51
Sriram
  • 53
  • 7
  • There is something that doesn't sit right with me here. If you are the one creating "nestedCollection_1" it means you already made it to be `Array[Map[String, Map[String, String]]`, right? Are you reading the nestedCollection as a serialized object? – marios Jan 24 '16 at 23:09
  • The "nestedCollection_1" here i have hard-coded for testing purpose . later i will be assigning "nestedCollection_1" with value by reading from file. The answer given by Łukasz is what i was expecting . – Sriram Jan 25 '16 at 01:24

1 Answers1

1

I am not sure what exacltly do you mean. Compiler can ensure type of any variable if you just annotate the type:

val nestedCollection_2: Map[String, List[String]] = Map(
  "key_3"-> List("key_33", "value_33"),
  "key_4"-> List("key_44", "value_44"))

You can see type of variable in scala repl when you define it, or using Alt + = in Intellij Idea.

scala> val nestedCollection_2 = Map(
     |   "key_3"-> List("key_33", "value_33"),
     |   "key_4"-> List("key_44", "value_44"))
nestedCollection_2: scala.collection.immutable.Map[String,List[String]] = Map(key_3 -> List(key_33, value_33), key_4 -> List(key_44, value_44))

Edit

I think I get your question now. Here is how you can get type as String:

import scala.reflect.runtime.universe._
def typeAsString[A: TypeTag](elem: A) = {
  typeOf[A].toString
}

Test:

scala> typeAsString(nestedCollection_2)
res0: String = Map[String,scala.List[String]]
scala> typeAsString(nestedCollection_1)
res1: String = scala.Array[(String, scala.collection.immutable.Map[String,String])]
Łukasz
  • 8,555
  • 2
  • 28
  • 51
  • I read the input from a file and the expected format of the input is in another file. I need to ensure that the input read from the file matches the expected format that is defined in another file – Sriram Jan 24 '16 at 16:07
  • You said you read values from variables, and now you say you read them from file. So how do you covert the string from file to a variable of unknown to you type? What is its format? – Łukasz Jan 24 '16 at 16:11
  • Sorry for the confusion....input is stored directly in a variable and the expected format is stored in a file...I will read the expected format from file and check if the input variable matches with expected format that I read from file – Sriram Jan 24 '16 at 16:16
  • Awesome thanks :) the edited answer was what i was expecting :) :) – Sriram Jan 25 '16 at 01:22