Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Last active March 9, 2021 07:22
Show Gist options
  • Select an option

  • Save nomisRev/8f10a30f4f087d24a006a0b6093b4c8f to your computer and use it in GitHub Desktop.

Select an option

Save nomisRev/8f10a30f4f087d24a006a0b6093b4c8f to your computer and use it in GitHub Desktop.
EitherEffect require syntax
import arrow.core.Either
import arrow.core.computations.EitherEffect
import arrow.core.computations.either
suspend fun <E> EitherEffect<E, *>.require(test: Boolean, fail: () -> E): Unit =
if (test) Unit
else Either.Left(fail()).bind()
suspend fun <E> EitherEffect<E, *>.error(error: E): Unit =
Either.Left(error).bind()
object CustomError
suspend fun main() {
val x: Either<CustomError, Long> = either {
require(10 > 1) { CustomError }
error(CustomError)
1L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment