List.empty vs. List() vs. new List()

前端 未结 3 661
你的背包
你的背包 2021-02-05 03:25

What the difference between List.empty, List() and new List()? When should I use which?

3条回答
  •  执笔经年
    2021-02-05 03:49

    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.

提交回复
热议问题