Last active
August 29, 2015 14:23
-
-
Save SteveBate/6440ef07620d2b485937 to your computer and use it in GitHub Desktop.
A simple example touching on Implicits and Options much like C# extensions and F# Options
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
object Main { | |
implicit class Converter(val s: String) { | |
def MaybeInt: Option[Int] = { | |
try { | |
Some(s.toInt) | |
} | |
catch { | |
case e: NumberFormatException => None | |
} | |
} | |
} | |
def main(args: Array[String]) { | |
"123".MaybeInt match { | |
case Some(x) => println(x) | |
case None => println("not a number") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment