Skip to content

Instantly share code, notes, and snippets.

@satendrakumar
Created March 23, 2017 12:44
Show Gist options
  • Save satendrakumar/6886f20226f04b08571eaec76a6e2c57 to your computer and use it in GitHub Desktop.
Save satendrakumar/6886f20226f04b08571eaec76a6e2c57 to your computer and use it in GitHub Desktop.
Generic Type Converter
object GenericTypeConverter extends App{
def convert[T: ClassTag](value:Any): Option[T] = {
val ct = implicitly[ClassTag[T]]
value match {
case ct(x) => Some(x)
case _ => None
}
}
def checkType[T: Manifest](t: T): Manifest[T] = manifest[T]
val number:Any = 2
val convertedValue = convert[Int](number).get
println(checkType(convertedValue))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment