A Sealed Result Type
Model success and failure without throwing everywhere.
A Sealed Result Type is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 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 Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Failures Are Normal
Networks drop and data goes missing. In shared code you want to model these as expected outcomes, not surprise crashes the apps must catch.
Why Not Just Throw?
Throwing exceptions across the Kotlin-to-Swift bridge is fragile. A returned value describing success or failure is far easier for both apps to consume. 🙂
Meet the Sealed Class
A sealed class defines a closed set of subtypes. The compiler knows every case, so your result can only ever be one of the variants you declared.
sealed class Result<out T>A Success Variant
One variant carries the value you wanted. We call it Success and give it a data property holding the result the apps will display.
data class Success<T>(val data: T) : Result<T>()A Failure Variant
The other variant carries what went wrong. Failure holds a message or cause so the UI can explain the problem to the user.
data class Failure(val message: String) : Result<Nothing>()Returning a Result
Your shared function now returns a Result instead of throwing. The caller receives one clear value describing the whole outcome at once.
fun load(): Result<User> = Success(user)Exhaustive when
Because the class is sealed, a when over it needs no else branch. Add a new variant later and the compiler flags every place you must update.
when (r) {
is Success -> show(r.data)
is Failure -> warn(r.message)
}Smart Casts for Free
Inside each branch Kotlin smart casts the result to that subtype. You can read Success.data or Failure.message with no manual casting at all.
Generic Over the Payload
The type parameter T lets one Result type wrap any payload, from a User to a list of orders, while Failure stays the same everywhere.
It Lives in commonMain
Define this Result in commonMain so Android and iOS share the exact same outcome type. No drift, one contract, both apps agree.
A Loading State Too
Many screens add a third variant, Loading, so the same sealed type drives spinners, content and errors from a single source.
object Loading : Result<Nothing>()Quick Check
Why does a sealed Result type help your shared code?
Recap
You modeled outcomes as a sealed Result with Success and Failure, returned values instead of throwing, and let the compiler enforce every case. 🎉
Frequently asked questions
Is the “A Sealed Result Type” lesson free?
Yes — the full text of “A Sealed Result Type” is free to read here on the web, and the Kotlin Multiplatform 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 Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “A Sealed Result Type”?
Model success and failure without throwing everywhere. You practise Kotlin Multiplatform 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 Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Sealed Result Type” 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 Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform 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
- A Sealed Result Type
- Catch Network & Parsing Errors
- Map Errors to UI Messages
- Retry & Offline Fallbacks