What the difference between List.empty
, List()
and new List()
? When should I use which?
First of all, new List()
won't work, since the List
class is abstract. The other two options are defined as follows in the List object:
override def empty[A]: List[A] = Nil
override def apply[A](xs: A*): List[A] = xs.toList
I.e., they're essentially equivalent, so it's mostly a matter of style. I prefer to use empty
because I find it clearer, and it cuts down on parentheses.