CodeBlanks Logo Icon CodeBlanks

Sealed and result

Learn a Result-like pattern with sealed classes.

The Result pattern

To carry an operation outcome, you can build a Result pattern with a sealed class. This pattern expresses success and failure as distinct types.

Example: sealed class Result; data class Success(val value: String) : Result(); data class Failure(val reason: String) : Result(). A function returns Result; the caller handles both cases with when. This is common in APIs that prefer carrying the outcome explicitly over throwing exceptions. The error case becomes data; control flow stays linear.

Success and Failure

In the Result pattern, Success carries the successful value and Failure carries the failure reason. Both are data classes; each has its own field.

Example: fun handle(r: Result): String = when (r) { is Success -> "ok: ${r.value}"; is Failure -> "err: ${r.reason}" }. In the is Success branch, r.value is accessed via smart cast; in the is Failure branch, r.reason is. This handles success and failure in the same function with a single return type. It is clearer and less error-prone than exception handling.

Practice The Result sealed class and the handle function

Define a sealed class named Result. Add two data class subtypes: Success(val value: String) and Failure(val reason: String), both inheriting from Result(). Write a function handle(r: Result): String whose body uses when (r) to return "ok: ${r.value}" for Success and "err: ${r.reason}" for Failure. In main create a Success("200") instance and print the result of handle.

Starter code

Main.kt
// Define the Result sealed class
// data class Success and Failure subtypes
// handle function: branch with when
// instantiate Success in main, print handle

Sample solution

Main.kt
sealed class Result
data class Success(val value: String) : Result()
data class Failure(val reason: String) : Result()

fun handle(r: Result): String {
    return when (r) {
        is Success -> "ok: ${r.value}"
        is Failure -> "err: ${r.reason}"
    }
}

fun main() {
    val r: Result = Success("200")
    println(handle(r))
}

Expected output

ok: 200

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 <code>sealed class Result</code> and <code>data class Success(val value: String) : Result()</code> are defined. If a function returns <code>Result</code>, what does it do?

  • It carries a success or failure outcome Correct answer
  • It always returns Success
  • It throws a runtime error
  • It only returns a String

Explanation: Result carries a success or failure outcome.

2 Match the Result pattern

Term Meaning
sealed class Result Closed base class
data class Success Success state
data class Failure Failure state
when (r) Branch by result

3 Complete the Result subtype definition

Question
data class Failure(val reason: String) : ____()
Answer
data class Failure(val reason: String) : Result()

Explanation: Failure inherits from the Result sealed class.

All lessons in Intermediate