0Pricing
Kotlin Academy · Lesson

Option and Nullable: When to Use Each

Compare Arrow's Option type with Kotlin nullable types and choose appropriately.

Option and Nullable: When to Use Each is a free Kotlin Academy lesson on CoddyKit — lesson 3 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.

Two Ways to Represent Absence

Kotlin has built-in nullable types (T?) that represent "a value or nothing." Arrow also provides Option — a sealed type with Some(value: A) and None. Both solve the same problem but fit different contexts.

Kotlin Nullable Types

val name: String? = findUser(id)?.name
val display = name ?: "Anonymous"

Arrow's Option<A>

import arrow.core.Option
import arrow.core.Some
import arrow.core.None
import arrow.core.toOption

val opt: Option<String> = "hello".toOption()   // Some("hello")
val absent: Option<String> = None

Converting Between Option and Nullable

val opt: Option<String> = nullableString.toOption()
val back: String? = opt.getOrNull()

map and flatMap on Option

val length: Option<Int> = "hello".toOption().map { it.length }  // Some(5)
val noneLength: Option<Int> = (null as String?).toOption().map { it.length }  // None

filter on Option

val positiveAge: Option<Int> = 25.toOption().filter { it > 0 }  // Some(25)
val rejectedAge:  Option<Int> = (-1).toOption().filter { it > 0 }  // None

fold: Consuming Option

val result: String = someOption.fold(
    ifEmpty = { "Nothing here" },
    ifSome  = { value -> "Got: $value" }
)

When to Prefer Nullable T?

When to Prefer Option<A>

Option Inside either { }

fun findUserOpt(id: Long): Option<User> = TODO()

fun getUser(id: Long): Either<UserError, User> = either {
    findUserOpt(id)
        .toEither { UserError.NotFound }
        .bind()
}

The Option Anti-Pattern

Quick Check

Recap: Option and Nullable

Frequently asked questions

Is the “Option and Nullable: When to Use Each” lesson free?

Yes — the full text of “Option and Nullable: When to Use Each” 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 “Option and Nullable: When to Use Each”?

Compare Arrow's Option type with Kotlin nullable types and choose appropriately. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Option and Nullable: When to Use Each” 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