docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

Scala 3 book - can you match on Any or not?

Open Sciss opened this issue 4 years ago • 2 comments

When type hierarchy is introduced: https://docs.scala-lang.org/scala3/book/first-look-at-types.html

it is said that

…it means that we cannot pattern match on values of type Any, but only on values that are a subtype of Matchable.

The type Matchable is used in the example here: https://docs.scala-lang.org/scala3/book/why-scala-3.html

def isTruthy(a: Matchable) = a match
  case 0 | "" => false
  case _ => true

But here, the traditional Any is used: https://docs.scala-lang.org/scala3/book/taste-control-structures.html

def getClassAsString(x: Any): String = x match
  case s: String => s"'$s' is a String"
  case i: Int => "Int"
  case d: Double => "Double"
  case l: List[_] => "List"
  case _ => "Unknown"

Which is confusing.

Sciss avatar May 18 '21 21:05 Sciss

And here: https://docs.scala-lang.org/scala3/book/methods-most.html

Sciss avatar May 18 '21 22:05 Sciss

I can pick this up.

chaitanyawaikar avatar May 19 '21 09:05 chaitanyawaikar