Created
October 28, 2013 06:56
-
-
Save f81/7192394 to your computer and use it in GitHub Desktop.
第14章:ScalaのOption型とnullを語る ref: http://qiita.com/f81@github/items/7bca48469d9aea65780d
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
scala> val map = Map(1 -> "Moses", 2 -> "Lucas", 3 -> "Henderson", 5 -> null) | |
map: scala.collection.immutable.Map[Int,String] = Map(1 -> Moses, 2 -> Lucas, 3 -> Henderson, 5 -> null) | |
scala> map get 1 | |
res0: Option[String] = Some(Moses) | |
scala> map get 3 | |
res1: Option[String] = Some(Henderson) | |
scala> map get 4 | |
res2: Option[String] = None |
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
scala> map get 5 | |
res3: Option[String] = Some(null) |
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
scala> val person = map get 2 | |
person: Option[String] = Some(Lucas) | |
scala> person.get | |
res3: String = Lucas |
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
scala> val nonePerson = map get 4 | |
nonePerson: Option[String] = None | |
scala> nonePerson.get | |
java.util.NoSuchElementException: None.get | |
at scala.None$.get(Option.scala:313) | |
at scala.None$.get(Option.scala:311) | |
at .<init>(<console>:10) | |
at .<clinit>(<console>) | |
at .<init>(<console>:7) | |
at .<clinit>(<console>) | |
at $print(<console>) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:606) | |
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734) | |
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) | |
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) | |
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) | |
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) | |
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756) | |
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801) | |
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713) | |
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577) | |
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584) | |
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587) | |
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878) | |
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) | |
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) | |
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135) | |
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833) | |
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83) | |
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96) | |
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105) | |
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) |
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
$ scalac Option1.scala | |
$ scala Option1 | |
Lucas | |
Not exist. |
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
$ scalac Option2.scala | |
Option2.scala:7: warning: match may not be exhaustive. | |
It would fail on the following input: None | |
o match { | |
^ | |
one warning found |
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 Option1 { | |
def main(args: Array[String]): Unit = { | |
val map = Map(1 -> "Moses", 2 -> "Lucas", 3 -> "Henderson") | |
def check(o: Option[String]) { | |
o match { | |
case Some(s) => println(s) | |
case None => println("Not exist.") | |
} | |
} | |
val some = map get (2) | |
val none = map get (4) | |
check(some) | |
check(none) | |
} | |
} |
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 Option2 { | |
def main(args: Array[String]): Unit = { | |
val map = Map(1 -> "Moses", 2 -> "Lucas", 3 -> "Henderson") | |
def check(o: Option[String]) { | |
o match { | |
case Some(s) => println(s) | |
} | |
} | |
val some = map get (2) | |
check(some) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment