Sealed class giới hạn một class hierarchy thành tập hợp subclass cố định, được định nghĩa trong cùng một file hoặc package.
- Rất phù hợp để biểu diễn các trạng thái giới hạn như response API (Success/Error/Loading).
- Khi dùng với
when, compiler bắt buộc xử lý hết tất cả subclass, không cần nhánhelse, giúp code an toàn hơn rất nhiều.
Sealed classes restrict a class hierarchy to a known set of subclasses defined in the same file or package.
- They're perfect for representing restricted type hierarchies like API responses (Success/Error/Loading).
- When used with
whenexpressions, the compiler forces you to handle all possible subclasses, eliminating the need for anelseclause.