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 Nullable Types
Kotlin's T? is the idiomatic choice for absence in most Kotlin code. The compiler enforces null checks, the safe-call operator ?. chains calls gracefully, and ?: "default" provides fallbacks concisely:
val name: String? = findUser(id)?.name
val display = name ?: "Anonymous"Arrow's Option<A>
Option wraps a nullable concept in a proper algebraic type with map, flatMap, filter, fold, and more. It integrates naturally with Arrow's functional pipeline style:
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> = NoneConverting Between Option and Nullable
Convert freely: value?.toOption() wraps a nullable into Option; option.getOrNull() extracts the value or null; option.getOrElse { default } provides a fallback:
val opt: Option<String> = nullableString.toOption()
val back: String? = opt.getOrNull()map and flatMap on Option
map { } transforms a Some value (returns None unchanged). flatMap { } chains operations that themselves return Option:
val length: Option<Int> = "hello".toOption().map { it.length } // Some(5)
val noneLength: Option<Int> = (null as String?).toOption().map { it.length } // Nonefilter on Option
filter { predicate } converts a Some to None if the predicate is false:
val positiveAge: Option<Int> = 25.toOption().filter { it > 0 } // Some(25)
val rejectedAge: Option<Int> = (-1).toOption().filter { it > 0 } // Nonefold: Consuming Option
fold(ifEmpty, ifSome) handles both cases in one expression without pattern matching:
val result: String = someOption.fold(
ifEmpty = { "Nothing here" },
ifSome = { value -> "Got: $value" }
)When to Prefer Nullable T?
Prefer Kotlin nullable (T?) when:
- You are writing idiomatic Kotlin code that others will read
- You need null-safe operator chains (
?.,?:) - The type is used in APIs that don't use Arrow
- Performance is critical (no boxing overhead)
When to Prefer Option<A>
Prefer Option when:
- You are already using Arrow's functional pipeline (
map,flatMap) - You want to compose optional values with
EitherorRaise - You want to make absence explicit in a generic functional context
- You are building a library where callers may not use Kotlin
Option Inside either { }
Inside Arrow's either { } block, you can call .bind() on an Option by first converting it to 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
Do not use Option everywhere just because it exists. In Kotlin, String? is clearer than Option in most application code. Reserve Option for when its functional operators add genuine value.
Quick Check
What does None.map { it.length } return in Arrow's Option?
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
- 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