有什么区别List.empty
, List()
and new List()
?我什么时候应该使用哪个?
首先,new List()
不会工作,因为List
类是抽象的。另外两个选项定义如下the List object http://www.scala-lang.org/api/current/scala/collection/immutable/List%24.html:
override def empty[A]: List[A] = Nil
override def apply[A](xs: A*): List[A] = xs.toList
也就是说,它们本质上是等效的,所以这主要是风格问题。我更喜欢使用empty
因为我发现它更清晰,而且减少了括号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)