Youssef Shoaib [MOD]
12/28/2025, 5:17 PMAlejandro Serrano.Mena
12/28/2025, 8:35 PMUlrich Schuster
12/28/2025, 8:52 PMNonEmptyMutableList? A Kotlin List is immutable, so I would expect a NonEmptyList to be the same (immutability is the default).Youssef Shoaib [MOD]
12/28/2025, 8:53 PMList is not immutable, it's read only. Case in point: val list: List<Int> = mutableListOf()Ulrich Schuster
12/28/2025, 8:55 PMYoussef Shoaib [MOD]
12/28/2025, 8:57 PMval mutable = mutableListOf(1)
val nonEmpty = mutable.wrapAsNonEmptyListOrThrow()
println(nonEmpty) // [1]
mutable.add(2)
println(nonEmpty) // [1, 2]
(note that wrapAsNonEmptyListOrThrow has an opt-in because you must ensure that the list you're wrapping never becomes empty)Ulrich Schuster
12/28/2025, 9:01 PMYoussef Shoaib [MOD]
12/28/2025, 9:03 PMwrapAsNonEmptyListOrThrow and opts-in (to the aptly named PotentiallyUnsafeNonEmptyOperation) does.
These functions are provided firstly for performance, but it's also perfectly legitimate to wrap a mutable list, as long as you, the person who explicitly opts-in, decides that your list will never become empty in the futureYoussef Shoaib [MOD]
12/28/2025, 9:04 PMNonEmptyMutableList to be a thing, and truly ensuring that the list cannot ever get emptyUlrich Schuster
12/28/2025, 9:05 PMYoussef Shoaib [MOD]
12/28/2025, 9:05 PMtoNonEmptyList() functions do a defensive copy.Youssef Shoaib [MOD]
12/28/2025, 9:10 PMPotentiallyUnsafeNonEmptyOperation, all your NonEmptyList-related code will never encounter empty lists (modulo bugs, of course).
If you opt-in to PotentiallyUnsafeNonEmptyOperation, it's your responsibility to make sure that the list passed to wrapAsNonEmptyXXX never goes empty. If you're a library, be dang sure that it doesn't, or your users will be very unhappy!
Within Arrow itself, we use that method sparingly, and (AFAIK) never to wrap a mutable list (I just remembered one exception, but it's in experimental Accumulate code, and it only calls add and addAll on the mutable list, so it's safe)
In fact, using the wrapAsNonEmptyXXX methods with a guaranteed-immutable list as the receiver is completely safe (of course, the orThrow variants will throw in such cases, which is to be expected).Youssef Shoaib [MOD]
12/29/2025, 11:04 PMbuildNonEmptyList/Set