Non-FP Kotlin makes easy uses of `require()`, `che...
# arrow
c
Non-FP Kotlin makes easy uses of
require()
,
check()
and
error()
to shortcut execution. Either is much more “precise” and “safe”, however I don't know how to write something as concise and readable:
Copy code
fun foo(param: Int) {
  require(param > 1) { "Some message here" }
Copy code
doSomething() ?: error("that's not possible")
}
I guess the
either
block should help with this, but I don't really see how to use it for these cases.
Maybe adding a function like this?
Copy code
fun <L> requireEither(cond: Boolean, block: () -> L): Either<L, Unit> =
  if (cond) Right(Unit)
  else Left(block())
This function would be used in an
either
block exactly as the current
require
function is used. Obviously the name is bad, but apart from that, what do you think?
They could probably be made
inline
as well, but yeah I'd love to have access to those ^^
s
Yes, can be made inline too 😄
Adding them locally for now would be super easy as you can see
c
Yep, I'm probably going to add them in my projects because they make validation much, much nicer than having ifs inside of ifs. Without
either
I would've used early returns, but those are not available everywhere (inside
crossinline
lambdas for example)
👍 1
s
Yes, I think it makes a lot of sense too! Feel free to add a ticket on the Arrow repo 😉
c
Nice! That looks really good
s
Feel free to comment any feedback on the PR! Really awesome suggestion 🙏