Surely a dupe, but hastily:
scala> :pa
// Entering paste mode (ctrl-D to finish)
import reflect._
case class Foo[A](x: A) {
def get[T: ClassTag]: Option[T] = x match {
case x: T => Some(x) // if x is of type T i.e. T =:= A
case _ => None
}
}
// Exiting paste mode, now interpreting.
import reflect._
defined class Foo
scala> val test = Foo("hi")
test: Foo[String] = Foo(hi)
scala> test.get[Int]
res0: Option[Int] = None
scala> test.get[String]
res1: Option[String] = Some(hi)