Created
November 8, 2012 20:59
-
-
Save kareblak/4041559 to your computer and use it in GitHub Desktop.
StringConverter
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
case class TjalleBalle(dblOpt: Option[Double]) | |
//Og her skal det skje magi | |
TjalleBalle("1.080") // men det gjør det altså ikke, og den whiner på feil type. | |
case class StringConverter[T](f: String => T) | |
implicit val convDouble = StringConverter[Double](_.toDouble) | |
implicit val convInt = StringConverter[Int](_.toInt) | |
implicit val convString = StringConverter[String](_) | |
implicit def optionify[T](s: String): Option[T] = try { | |
Some(implicitly[StringConverter[T]].f(s)) | |
} catch { | |
case _ => None | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment