Created
March 23, 2017 12:44
-
-
Save satendrakumar/6886f20226f04b08571eaec76a6e2c57 to your computer and use it in GitHub Desktop.
Generic Type Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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