List(1,2) match {
case List(1,_) => println("1 in postion 1")
case _ => println("default")
}
compiles / works fine.
So do
List(1) match ...
List(3,4,5) match ...
but not
List() match ...
which results in the following error
found : Int(1)
required : Nothing
case List(1,_) => println("1 in postion 1")
Why does List() try to match List(1,_)?
When you write
List(), the type inferred isNothing, which is subtype to everything.What is happening is that Scala gives an error when you try impossible matches. For example,
"abc" match { case 1 => }will result in a similar error. Likewise, becauseList(1, _)can be statically determined to never matchList(), Scala gives an error.