Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.31 KB | None | 0 0
  1. object Heltal {
  2.   def unapply(x: String): Option[Int] = try {
  3.     Some(x.toInt)
  4.   } catch {
  5.     case e: NumberFormatException => None
  6.   }
  7. }
  8.  
  9. def runMatch(s: String) = s match {
  10.   case Heltal(x) => println(x + 1)
  11.   case _         => println("NaN")
  12. }
  13.  
  14. @ runMatch("hi")
  15. NaN
  16.  
  17. @ runMatch("10")
  18. 11
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement