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,_)?
List()
has type List[Nothing]
. If you use List[Int]()
it will work as you expect.
(In general, types are as restrictive as they can possibly be; since you have made a list with nothing in it, the most-restrictive-possible type Nothing
is used instead of Int
as you intended.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With