Senin, 10 Januari 2011

Switch Scala

$ cat Test1.scala
import scala.io._

def toInt(in: String): Option[Int] =
try {
Some(Integer.parseInt(in.trim))
} catch {
case e: NumberFormatException => None
}

val a = toInt("102");
println(a);
println("The value is " + a.get); // the value can be retrieved by Option#get

val b = toInt("A");
println(b);

$ scala Test1.scala
Some(102)
The value is 102
None

Tidak ada komentar:

Poskan Komentar