James Baker
06/29/2023, 3:18 PMNonEmptyList 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()James Baker
06/29/2023, 3:19 PMNonEmptySet to NonEmptyList?Alejandro Serrano Mena
06/29/2023, 7:14 PMNonEmptyCollection interface (maybe just with a first() returning T), make both types implement that interface, and then make toNonEmptySet and toNonEmptyList work on thosesindrenm
06/30/2023, 12:37 AMtoNonEmptySet sounds good to me. If you also want to add an extension on any potentially-including-nulls collection, then that'd be something like toNonEmptySetOrNull, IMO).sindrenm
06/30/2023, 12:39 AMtoSet is great, just because we already know Kotlin's sets to potentially be empty. Expressive is better, in this case, IMO.Alejandro Serrano Mena
06/30/2023, 10:08 AMNonEmptyCollection and the functions you mentioned https://github.com/arrow-kt/arrow/pull/3068James Baker
06/30/2023, 1:23 PM