Question | Click to View Answer |
What does the following code print? val x: Option[String] = Some("hi")
println(x.get)
|
"hi" is printed. The |
What does the following code print? var y: Option[String] = None
y.get
|
This code throws a
|
What does the following code print? val firstName: Option[String] = Some("Frank")
println(firstName.getOrElse("Sue"))
|
"Frank" is printed. The |
What does the following code print? val bestNumber: Option[Int] = None
println(bestNumber.getOrElse(8))
|
8 is printed. The |
What does the following code print? var myMood: Option[String] = Some("happy")
myMood = Some(10)
println(myMood.get)
|
This throws a type mismatch error.
|
What does the following code print? val something = null
def getLength(x: String): Int = x.length
println(getLength(something))
|
This code returns a This code ends up calling The Scala Some / Option / None classes exist so you can avoid
|
What does the following code print? val something = null
def crappyGetLength(x: String): Any =
if (x != null) x.length else null
println(crappyGetLength(something))
|
This code prints The Some, Option, and None exist so you can avoid writing code that directly references |
What does the following code print? def betterGetLength(x: Option[String]): Option[Int] =
if (x.isDefined) Some(x.get.length) else None
println(betterGetLength(None))
println(betterGetLength(Some("puppy")))
|
This is better than using |
What does the following code print? def goodGetLength(x: Option[String]): Option[Int] =
x.map(_.length)
println(goodGetLength(None))
println(goodGetLength(Some("puppy")))
|
This is an idiomatic way to deal with optional values in Scala. You're probably used to seeing the Think of optional values as lists that can contain one or no values. Think of it like iterating over an empty list or iterating over a list that contains one value. When you think of optional values like lists that contain zero or one element, the |
Let's write some code that'll create a collection of dogs, will fetch one of the dogs, and will print an attribute of the dog. What does the following code print? case class Dog(
id: Int,
firstName: String,
nickName: Option[String])
val dog1 = Dog(1, "Spot", Some("foofie"))
val dog2 = Dog(2, "Luna", None)
val dogs = Map(
1 -> dog1,
2 -> dog2)
def findById(dogs: Map[Int, Dog], id: Int): Option[Dog] =
dogs.get(id)
val d1 = findById(dogs, 1)
println(d1.map(_.nickName))
|
You don't typically want to write code that returns nested |
How can you finish this code to return case class Dog(
id: Int,
firstName: String,
nickName: Option[String])
val dog1 = Dog(1, "Spot", Some("foofie"))
val dog2 = Dog(2, "Luna", None)
val dogs = Map(
1 -> dog1,
2 -> dog2)
def findById(dogs: Map[Int, Dog], id: Int): Option[Dog] =
dogs.get(id)
val d1 = findById(dogs, 1)
|
Notice that we're using |
What does the following code print? case class Cat(name: String, age: Int)
val darla = Some(Cat("darla", 12))
println(darla.filter(_.age > 10))
|
You can filter optional values. Scala returns the value if the predicate is true. In this case, the |
What does the following code print? case class Cat(name: String, age: Int)
val chunkers = Some(Cat("chunkers", 4))
println(chunkers.filter(_.age > 10))
|
When you filter over an optional values and the predicate evaluates to false, Scala returns |
What does the following code print? case class Cat(name: String, age: Int)
val nala: Option[Cat] = None
println(nala.filter(_.age > 10))
|
The You should start to be seeing how Scala's option handling methods like |
Why should |
The Scala community discourages using |