Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有人可以解释为什么我应该使用 Scalaz 的NonEmptyList而不是 Scala 的List吗?
NonEmptyList
List
在不可变的应用程序中,创建一个空的没有多大意义List
Scala 的集合有许多不安全的方法。这些包括head,last等。不安全意味着如果集合为空,它们将抛出异常。现在你可以说“我非常确定这个集合在运行时不会为空,所以我的代码是安全的”。但是,有人出现,更改您的代码等。
因此,从本质上讲,scalaz 类型为您提供了静态安全性,因为如果您静态地知道集合不会为空,那么调用 head 等是安全的。