I'm thinking about making a contribution to simplify converting a
NonEmptyList
to a
NonEmptySet
.
Currently, I think the way to do this is:
nonEmptyList.toNonEmptySetOrNull()!!
. This isn't ideal because the
orNull
case can never happen leading to unnecessary error handling (in this case with the
!!
).
Is this something people would be interested and if so which of these is preferred?
Option 1: Have a
toSet()
function in the class body, so all
toSet()
calls return a a
NonEmptySet
(I like this option because the
Set
will always be "`NonEmpty`", this would change the behaviour of existing code but it wouldn't be a breaking change):
// Sudocode implementation
class NonEmptyList<out A> {
fun toSet(): NonEmptySet<A> = NonEmptySet(head, tail.toSet())
}
// Usage
nonEmptyList.toSet()
Option 2: Have an extension function
toNonEmptySet()
(more explicit and maybe a safer change than
Option 1):
// Implementation
fun <A> NonEmptyList<A>.toNonEmptySet(): NonEmptySet<A> = NonEmptySet(head, tail.toSet())
// Usage
nonEmptyList.toNonEmptySet()