이창언
이창언

Reputation: 1

warning: unreachable code in Scala repl

this code is in "scale-exercises.org" site

class Car(val make: String, val model: String, val year: Short, val topSpeed: Short)
class Employee(val firstName: String, val middleName: Option[String], val lastName: String)

object Tokenizer {
  def unapply(x: Car) = Some(x.make, x.model, x.year, x.topSpeed)

  def unapply(x: Employee) = Some(x.firstName, x.lastName)
}

val result = new Employee("Kurt", None, "Vonnegut") match {
  case Tokenizer(c, d) ⇒ "c: %s, d: %s".format(c, d)
  case _ ⇒ "Not found"
}

return : warning: unreachable code case _ ⇒ "Not found"

why is warning code ?? please reply ...

Upvotes: 0

Views: 238

Answers (1)

sepp2k
sepp2k

Reputation: 370162

Since unapply's return type is Some, Scala knows that the match will never fail and the subsequent cases can never be reached.

If you explicitly declare the return type as Option, the warning will disappear, but that won't change the fact that the case _ can never actually be reached.

Upvotes: 2

Related Questions