Hey, in this example from the docs: (<https://arro...
# arrow
j
Hey, in this example from the docs: (https://arrow-kt.io/learn/typed-errors/validation/#fail-first-vs-accumulation)
Copy code
either {
      zipOrAccumulate(
        { ensure(title.isNotEmpty()) { EmptyTitle } },
        { ensureNotNull(authors.toNonEmptyListOrNull()) { NoAuthors } }
      ) { _, _ ->
        Book(title, TODO())
      }
    }
What would be the best way of extracting the title not empty rule so it can be reused elsewhere, like this?
Copy code
private fun titleNotEmpty(title: String): RaiseAccumulate<BookValidationError>.() -> Unit = {
    ensure(title.isNotEmpty()) { EmptyTitle }
}

either {
      zipOrAccumulate(
        titleNotEmpty(title),
 ....
Also would I have to have the return type as RaiseAccumulate instead of Raise? I may want to use the rule in a non accumulating setting. (I know it's contrived but I'm using the example from the docs instead of my own code)
s
Hey @Jon Bailey, Sorry for the late reply. The most idiomatic way would be to use a extension function instead of a function that returns a lambda with receiver. It's a bit subjective, but I think everyone is more familiar with extension functions than lambdas with receivers.
Copy code
private fun Raise<BookValidationError>.titleNotEmpty(title: String): Unit =
    ensure(title.isNotEmpty()) { EmptyTitle }
That way you can just call
titleNotEmpty("...")
inside
zipOrAccumulate
and still re-use it in a non accumulating setting.