tezvyn:

When to use a sealed class instead of an enum?

Source: kotlinlang.orgadvanced

Tests your grasp of Kotlin's type hierarchies. Use `sealed class` for states with associated data (e.g., `Success(data)`), as subclasses can have unique properties. Use `enum` for simple, constant states. A red flag is treating them as interchangeable.

This tests your understanding of Kotlin's type system for modeling restricted hierarchies, like network states. A strong answer differentiates them by data: `enum` constants are single instances of the same type, best for simple, fixed states. `Sealed class` is superior when states carry different data (e.g., `Success(data)` vs. `Error(exception)`), as its subclasses can be unique data classes or objects. A red flag is only mentioning `when` exhaustiveness, which both offer, and missing the core difference in state representation.

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

When to use a sealed class instead of an enum? · Tezvyn