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

前端 未结 3 664
你的背包
你的背包 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:59

    From the source code of List we have:

    object List extends SeqFactory[List] {
      ...
      override def empty[A]: List[A] = Nil
      override def apply[A](xs: A*): List[A] = xs.toList
      ... 
    }
    
    case object Nil extends List[Nothing] {...}
    

    So we can see that it is exactly the same

    For completeness, you can also use Nil.

提交回复
热议问题