What the difference between List.empty
, List()
and new List()
? When should I use which?
For the creations of an empty list, as others have said, you can use the one that looks best to you.
However for pattern matching against an empty List, you can only use Nil
scala> List()
res1: List[Nothing] = List()
scala> res1 match {
| case Nil => "empty"
| case head::_ => "head is " + head
| }
res2: java.lang.String = empty
EDIT : Correction: case List()
works too, but case List.empty
does not compile