Is there any built-in way to ensure lists are empt...
# arrow
e
Is there any built-in way to ensure lists are empty, which gives a nel when it fails? Sort-of like
Copy code
ensureEmpty(list) { nel -> ProblemWithNel(nel) }
s
No, but I've had more of these questions and I think we should consider facilitating them. Adding useful functions to the DSL can benefit multiple types, and DSLs at once.
Some transformations, especially
NonEmpty
are hard since after the
isNotEmpty
or
isEmpty
check you don't know anything.
e
Want me to raise an issue? Or will you create one? 🙂 I guess you know more about what the desired API should look like. Personally I added this:
Copy code
@RaiseDSL
public inline fun <Error, B : Any> Raise<Error>.ensureEmpty(
   value: Collection<B>,
   raise: (NonEmptyList<B>) -> Error,
) {
   value.toNonEmptyListOrNull()?.let { nel ->
      raise(raise(nel))
   }
}
s
That's perfect for a impl/solution ☺️ Yes, please create a ticket. Not sure if we'll add this one but I think it's good to have an open discussion. I know @timrijckaert once proposed the opposite.
ensureNotEmpty
for
Iterable
->
NonEmptyList
.
🫶 1
There were a couple more, but I cannot remember them.
e