Skip to content

Instantly share code, notes, and snippets.

@SteveBate
Last active August 29, 2015 14:23
Show Gist options
  • Save SteveBate/6440ef07620d2b485937 to your computer and use it in GitHub Desktop.
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
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