title: Scala asInstanceOf with parameterized types
body: <p>I would like to write a function that casts to type A, where A can be e.g. List[Int], or a more complicated parameterized type like Map[Int, List[Int]].</p>
  
  <pre><code>def castToType[A](x: Any): A = {
    // throws if A is not the right type
    x.asInstanceOf[A]
  }
  </code></pre>
  
  <p>Right now, due to type erasure (I believe), the code merrily works even when the type is not correct. The error only manifests on access, witha ClassCastException.</p>
  
  <pre><code>val x = List(1, 2, 3)
  val y = castToType[List[String]](x)
  y(0) --&gt; throws java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
  </code></pre>
  
  <p>Is there a way I can use manifests to make this work properly? Thanks!</p>