Skip to content

Instantly share code, notes, and snippets.

@relrod
Created October 4, 2014 19:16
Show Gist options
  • Select an option

  • Save relrod/208b58ce10ff30ba6164 to your computer and use it in GitHub Desktop.

Select an option

Save relrod/208b58ce10ff30ba6164 to your computer and use it in GitHub Desktop.
Scala is hilarious
ricky@t520 ~$ scala
Welcome to Scala version 2.11.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> Array("foo","bar")(2)
<console>:8: error: type mismatch;
found : Int(2)
required: scala.reflect.ClassTag[String]
Array("foo","bar")(2)
^
scala> (Array("foo","bar"))(2)
<console>:8: error: type mismatch;
found : Int(2)
required: scala.reflect.ClassTag[String]
(Array("foo","bar"))(2)
^
scala> val x = Array("foo","bar")
x: Array[String] = Array(foo, bar)
scala> x(2)
java.lang.ArrayIndexOutOfBoundsException: 2
... 33 elided
scala> x(1)
res3: String = bar
scala> x.lift(2)
res4: Option[String] = None
scala> x.lift(1)
res5: Option[String] = Some(bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment