prosseek
prosseek

Reputation: 191159

Removing "eliminated by erasure" warning in Scala

I have a simple Scala function that generates a Json file from a Map[String, Any].

  def mapToString(map:Map[String, Any]) : String = {
    def interpret(value:Any)  = {
      value match {
        case value if (value.isInstanceOf[String]) => "\"" + value.asInstanceOf[String] + "\""
        case value if (value.isInstanceOf[Double]) => value.asInstanceOf[Double]
        case value if (value.isInstanceOf[Int]) => value.asInstanceOf[Int]
        case value if (value.isInstanceOf[Seq[Int]]) => value.asInstanceOf[Seq[Int]].toString.replace("List(", "[").replace(")","]")
        case _ => throw new RuntimeException(s"Not supported type ${value}")
      }
    }
    val string:StringBuilder = new StringBuilder("{\n")
    map.toList.zipWithIndex foreach {
      case ((key, value), index) => {
        string.append(s"""  "${key}": ${interpret(value)}""")
        if (index != map.size - 1) string.append(",\n") else string.append("\n")
      }
    }
    string.append("}\n")
    string.toString
  }

This code works fine, but it emits a warning message in the compilation.

Warning:(202, 53) non-variable type argument Int in type Seq[Int] (the underlying of Seq[Int]) 
is unchecked since it is eliminated by erasure
        case value if (value.isInstanceOf[Seq[Int]]) => 
value.asInstanceOf[Seq[Int]].toString.replace("List(", "[").replace(")","]")
                                                ^

The line case value if (value.isInstanceOf[Seq[Int]]) causes the warning, and I tried case value @unchecked if (value.isInstanceOf[Seq[Int]]) to removed the warning, but it does not work.

How to remove the warning?

Upvotes: 1

Views: 1659

Answers (3)

prosseek
prosseek

Reputation: 191159

This is the better code that generates no warning message (with the hints from Thilo & Łukasz).

  def mapToString(map:Map[String, Any]) : String = {
    def interpret(value:Any)  = {
      value match {
        case value:String => "\"" + value + "\""
        case value:Double => value
        case value:Int => value
        case value:Seq[_] => value.mkString("[",",","]")
        case _ => throw new RuntimeException(s"Not supported type ${value}")
      }
    }
    map.toList.map { case (k, v) => s"""  "$k": ${interpret(v)}""" }.mkString("{\n", ",\n", "\n}\n")
  }

Upvotes: 1

Thilo
Thilo

Reputation: 262834

If you don't really care about the component type (and it seems you do not, as all you do is stringify it):

case value if (value.isInstanceOf[Seq[_]]) => 
    value.asInstanceOf[Seq[_]].toString.replace("List(", "[").replace(")","]")

Come to think of it, you should be able to call toString on anything anyway:

case value if (value.isInstanceOf[Seq[_]]) => 
    value.toString.replace("List(", "[").replace(")","]")

but instead of toString followed by messing with the String consider Seq#mkString

value.mkString("[", ",", "]")

Finally, that pattern isInstanceOf/asInstanceOf can be replaced by a match (in all the cases)

case value: Int => value    // it's an Int already now, no cast needed 
case value: Seq[_] => value.mkString("[", ",", "]")

Upvotes: 2

Johny T Koshy
Johny T Koshy

Reputation: 3922

You could do the following,

case value: String => ???
case value: Double => ???
case value: Int => ???
case value: Seq[Int] @unchecked => ???

or as @Thilo mentioned

case value: Seq[_] => 

Upvotes: 1

Related Questions