0Pricing
Kotlin Academy · Lesson

Arrow Raise DSL: Composing Typed Errors

Use Arrow's Raise context and raise() to compose multi-error flows cleanly.

Arrow Raise DSL: Composing Typed Errors is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Limitation of Either Chaining

Chaining Either with flatMap creates deeply nested lambdas when you have multiple sequential operations. Arrow's Raise DSL (introduced in Arrow 1.2) offers a cleaner, coroutine-style syntax using Kotlin's context receivers or extension functions.

What Is Raise<E>?

Raise is a context that can raise (short-circuit with) an error of type E. A function that can fail calls raise(error) to abort and propagate the error, similar to throwing but without exceptions.

The either { } Builder

The either { } builder creates an Either from a block that has a Raise in scope. Inside, you raise(error) for failure or simply return a value for success:

import arrow.core.raise.either
import arrow.core.raise.Raise

fun validateAge(age: Int): Either<String, Int> = either {
    if (age < 0) raise("Age cannot be negative")
    if (age > 150) raise("Age too large")
    age
}

bind() — Unwrapping Either Inside either{}

Inside an either { } block, call .bind() on any Either value to unwrap it. If it is a Left, bind() automatically raises and short-circuits the block:

fun createUser(name: String, age: Int): Either<String, User> = either {
    val validName = validateName(name).bind()  // raises if Left
    val validAge  = validateAge(age).bind()    // raises if Left
    User(validName, validAge)                  // only reached if both succeed
}

ensure() — Inline Condition Check

ensure(condition) { error } is a shorthand for if (!condition) raise(error). It keeps validation logic concise:

fun validateEmail(email: String): Either<String, String> = either {
    ensure(email.contains("@")) { "Email must contain @" }
    ensure(email.length <= 255) { "Email too long" }
    email
}

ensureNotNull() — Null Check with Raise

ensureNotNull(value) { error } unwraps a nullable value or raises if it is null:

fun findUser(id: Long, repo: UserRepo): Either<UserError, User> = either {
    ensureNotNull(repo.findById(id)) { UserError.NotFound(id) }
}

Raise with Context Receivers (Arrow 1.2+)

You can declare functions that require a Raise context directly, without wrapping in either { }:

context(Raise<String>)
fun requirePositive(n: Int): Int {
    ensure(n > 0) { "Must be positive" }
    return n
}

// Called inside either { }
val result = either { requirePositive(-1) }  // Left("Must be positive")

Accumulating Errors with zipOrAccumulate

By default, Raise short-circuits on the first error. Use zipOrAccumulate() to run multiple validations and collect all errors into a NonEmptyList:

val result: Either<NonEmptyList<String>, User> = either {
    zipOrAccumulate(
        { validateName(name).bind() },
        { validateAge(age).bind() }
    ) { validName, validAge -> User(validName, validAge) }
}

recover { } — Handling Specific Errors

Use recover { error -> ... } inside either { } to handle a specific raised error and provide a fallback value, resuming the computation:

val name: String = either {
    findUser(0L).bind().name
}.recover { error ->
    if (error == UserError.NotFound) "Guest" else raise(error)
}.getOrNull() ?: "Guest"

Raise vs Either Chaining: When to Use Each

Use either { }.bind() for linear flows with multiple sequential steps — it reads like imperative code but stays pure. Use flatMap chaining for short two-step compositions or when composing with other functional libraries.

Testing Raise-Based Functions

Test by calling the function inside either { } and asserting on the result. Arrow's shouldBeLeft() and shouldBeRight() Kotest matchers make assertions concise:

import arrow.core.shouldBeLeft
import arrow.core.shouldBeRight

@Test
fun `negative age returns Left`() {
    validateAge(-1).shouldBeLeft()
}

@Test
fun `valid age returns Right`() {
    validateAge(25).shouldBeRight(25)
}

Quick Check

Inside an either { } block, what does calling .bind() on an Either.Left value do?

Recap: Arrow Raise DSL

Key takeaways:

  • either { } creates an Either from a block with Raise in scope
  • .bind() unwraps Either; short-circuits on Left
  • ensure(condition) { error } and ensureNotNull(value) { error } for inline checks
  • zipOrAccumulate() to collect multiple errors instead of short-circuiting
  • Reads like imperative code while remaining purely functional

Frequently asked questions

Is the “Arrow Raise DSL: Composing Typed Errors” lesson free?

Yes — the full text of “Arrow Raise DSL: Composing Typed Errors” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arrow Raise DSL: Composing Typed Errors”?

Use Arrow's Raise context and raise() to compose multi-error flows cleanly. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arrow Raise DSL: Composing Typed Errors” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Either : Typed Error Handling Without Exceptions
  2. Arrow Raise DSL: Composing Typed Errors
  3. Option and Nullable: When to Use Each
  4. Functional Domain Modeling with Arrow's Core Types
← Back to Kotlin Academy