Advertisement
Guest User

Untitled

a guest
Sep 6th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.50 KB | None | 0 0
  1. object SafeString {
  2.   class StrOps(input: String) {
  3.     private def toOption[A](in: => A) = try {
  4.       Some(in)
  5.     } catch {
  6.       case _: Throwable => None
  7.     }
  8.  
  9.     def toInt: Option[Int] = toOption(input.toInt)
  10.     def toFloat: Option[Float] = toOption(input.toFloat)
  11.     def toDouble: Option[Double] = toOption(input.toDouble)
  12.     def toLong: Option[Long] = toOption(input.toLong)
  13.   }
  14.  
  15.   implicit class RichString(val left: String) extends AnyVal {
  16.     def opt: StrOps = new StrOps(left)
  17.   }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement