状態を表すEnumsとSealed Classes
固定された選択肢と結果のバリエーションを安全にモデル化します
「状態を表すEnumsとSealed Classes」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Model Fixed Choices
Some values have a known, limited set of options. Kotlin gives you enums and sealed classes to model them safely in shared code.
Meet the enum class
An enum class lists every allowed value by name. Nothing outside that list can ever sneak in.
enum class Status { ACTIVE, PAUSED, DONE }Use an enum Value
Refer to an option by its name. Because the set is closed, your state is always one of the listed cases.
val current = Status.ACTIVEEnums Can Carry Data
Give an enum constructor properties so each case can hold extra fixed info, like a label or numeric code.
enum class Plan(val price: Int) { FREE(0), PRO(9) }When Cases Need Different Data
Enums share one shape. When variants need different fields, reach for a sealed class instead.
Meet the sealed class
A sealed class defines a closed family of subtypes. Each subtype can carry its own distinct data.
sealed class Result
data class Ok(val value: String) : Result()
data class Err(val message: String) : Result()Why Sealed Is Powerful
Because the compiler knows every subtype, a sealed class models success, loading and error states with type-safe payloads.
val state: Result = Ok("loaded")Match with when
Handle each case using when. The compiler checks you covered every variant of the sealed type.
when (state) {
is Ok -> println(state.value)
is Err -> println(state.message)
}Exhaustive when, No else
When you handle all sealed cases, you can drop the else branch. Add a new subtype later and the compiler reminds you.
Shared, So Both Apps Agree
Defined in commonMain, these state types drive identical logic on Android and iOS with zero drift.
Pick the Right Tool
Use an enum for simple fixed options, and a sealed class when each case carries different data.
Quick Check
Which type should model these states?
Recap
Use shared enums for fixed options and sealed classes for variants with their own data, then match them safely with when.
よくある質問
「状態を表すEnumsとSealed Classes」レッスンは無料ですか?
はい。「状態を表すEnumsとSealed Classes」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「状態を表すEnumsとSealed Classes」で何を学びますか?
固定された選択肢と結果のバリエーションを安全にモデル化します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「状態を表すEnumsとSealed Classes」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ドメイン用のData Classes
- 状態を表すEnumsとSealed Classes
- 共有モデルのNull Safety
- モデルのValidation Helpers