Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Last active December 15, 2015 06:39
Show Gist options
  • Save ryanlecompte/5217497 to your computer and use it in GitHub Desktop.
Save ryanlecompte/5217497 to your computer and use it in GitHub Desktop.
val s = "hello"
def as[A: Manifest](v: Any): Option[A] = {
if (manifest[A].erasure.isInstance(v)) Some(v.asInstanceOf[A])
else None
}
as[Int](s).foreach { _ => println("worked as int") }
as[String](s).foreach { _ => println("worked as string") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment