Is there any value in adding `List<A>.asNonEmptyLi...
# arrow-contributors
p
Is there any value in adding
List<A>.asNonEmptyListUnsafe()
that avoids copying and simply wraps the list (potentially with a
require(isNotEmpty())
check? (same for NonEmptySet) - currently the suggested replacement for the old
Nel.fromListUnsafe
is
list.toNonEmptyListOrNull() ?: error(...)
however this currently iterates the list twice (effectively doing
iterator().let { listOf(it.next()) + Iterable { it }.toList() }
)
I understand the rationale to defensively copy the input list to some extent - otherwise nothing stops you from creating a nel wrapping a mutable list and then clearing it - but there may be times when copying may be undesirable but the semantics of a nel desirable
y
I wonder if pulling in
kotlinx-immutable-collections
may be worth it for Arrow? After all, it's an FP library, so immutable collections make sense. Anyone working with a
NonEmptyList
likely wants it to be immutable anyways. If we did that, we could then use
toImmutableList
and not even need an unsafe method at all.
p
an unsafe function with potentially an
@OptIn
annotation would allow for a clean integration point for
kotlinx-immutable-collections
without coupling
a
on the issue raised by @phldavies, this was based a bit on the experience of people thinking they know they can call the unsafe function, then calling it wrongly, and things exploding somewhere else. Maybe some API with an opt-in? about
kotlinx-immutable
, what about having an
arrow-core-immutable
which wraps those versions? That way people don't need to depend on the immutable library if they don't want...
(we try to keep dependencies really barebones because people really care about those things)
p
A separate module sounds good to me but would need the optin to allow calling the otherwise internal constructor.