Skip to content

Instantly share code, notes, and snippets.

@kareblak
Created November 8, 2012 20:59
Show Gist options
  • Save kareblak/4041559 to your computer and use it in GitHub Desktop.
Save kareblak/4041559 to your computer and use it in GitHub Desktop.
StringConverter
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