title: Scala check type of generics
body: <p>How do I do something like this in Scala?</p>
  
  <pre><code>case class Foo[A](x: A) {
  
    def get[T]: Option[T] = x match {
      case x: T =&gt; Some(x)    // if x is of type T i.e. T =:= A
      case _ =&gt; None
    }
  }
  
  val test = Foo("hi")
  assert(test.get[Int] == None)
  assert(test.get[String] == Some("hi"))
  </code></pre>
  
  <p>I tried this and ran into some weird time inference failure:</p>
  
  <pre><code>import scala.util.{Try, Success}
  import reflect._
  
  case class Foo[A](x: A) extends Dynamic {
  
    def get[T: ClassTag]: Option[T] = Try(x.asInstanceOf[T]) match {
      case Success(r) =&gt; Some(r) 
      case _ =&gt; None
    }
  }
  
  object Foo extends App {
    val test = Foo("hi")
    val wtf: Option[Int] = test.get[Int]
    assert(wtf.isInstanceOf[Option[String]])
    assert(wtf == Some("hi"))     // how????
    // val wtf2: Option[String] = wtf  // does not compile even if above assert passes!!
  }
  </code></pre>