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. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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 anEitherfrom a block withRaisein scope.bind()unwrapsEither; short-circuits onLeftensure(condition) { error }andensureNotNull(value) { error }for inline checkszipOrAccumulate()to collect multiple errors instead of short-circuiting- Reads like imperative code while remaining purely functional
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Arrow Raise DSL: Composing Typed Errors” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Arrow Raise DSL: Composing Typed Errors”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- Either : Typed Error Handling Without Exceptions
- Arrow Raise DSL: Composing Typed Errors
- Option and Nullable: When to Use Each
- Functional Domain Modeling with Arrow's Core Types