List
から延びないNonEmptyList
を有する主な理由は、APIの中の仮定を含むの現像経験あります。
最初に、::
にはすべての方法があります。誤解を招く可能性があり、より強力な前提でより良いAPIを設計することが難しくなります。さらに、List
には直接::
を返すメソッドがありません。つまり、開発者はを空ではなくの抽象化を手動で維持する必要があります。
// NonEmptyList usage is intuitive and types fit together nicely
val nonEmpty: NonEmptyList[Int] = NonEmptyList.of(1, 2, 3)
val biggerNonEmpty: NonEmptyList[Int] = 0 :: nonEmpty
val nonEmptyMapped: NonEmptyList[Int] = nonEmpty.map(_ * 2)
// :: has lots of problems
// PROBLEM: we can't easily instantiate ::
val cons: ::[Int] = 1 :: 2 :: 3 :: Nil // type mismatch; found: List[Int]; required: ::[Int]
val cons: ::[Int] = new ::[Int](1, ::(2, ::(3, Nil)))
// PROBLEM: adding new element to Cons returns List
val biggerCons: ::[Int] = 0 :: cons // type mismatch; found: List[Int]; required: ::[Int]
// PROBLEM: ::.map returns List
val consMapped : ::[Int] = cons.map(_ * 2) // type mismatch; found: List[Int]; required: ::[Int]
注NonEmptyList
、すなわちfilter
、filterNot
とcollect
、List
を返すメソッドを持っていること:
は、私はあなたに私が実際に何を意味するかを示す例をお見せしましょう。どうして? NonEmptyList
でフィルタリングすると、すべての要素を除外してリストが空になる可能性があるためです。
これは、全体をにするものではありません。抽象化が非常に強力です。関数の入出力型を適切に使用することで、APIに関する前提をエンコードできます。 ::
はこの抽象化を提供しません。