when a value can have one of the types from a limited set, but cannot have any other type. They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state. Definition of sealed classes from the official documentation
class Failure(val error: Throwable): Result() object Cancelled: Result() } val done = when (result) { is Result.Success -> showList(result.items) is Result.Failure -> result.error.printStackTrace() is Result.Cancelled -> hideStuff() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMessage: String) : SocialSignInResult() }
: SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMessage: String) : SignInResult() }
class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
: SocialSignInResult() data class Link(val token: String) : SocialSignInResult() data class Register(val token: String) : SocialSignInResult() data class Error(val errorMsg: String) : SocialSignInResult() } sealed class SignInResult { data class Success(val token: String) : SignInResult() data class TwoFactorAuth(val token: String) : SignInResult() data class Fail(val token: String) : SignInResult() data class RetrySocial(val errorMsg: String) : SignInResult() } sealed class TwoFAResult { object Success : TwoFAResult() data class Retry2FA(val token: String) : TwoFAResult() data class Fail(val errorMsg: String) : TwoFAResult() data class RetrySocial(val errorMsg: String) : TwoFAResult() }
class Success : SignIn() sealed class TwoFactor : SignIn() { class Success : TwoFactor() class RetryTwoFactor : TwoFactor() class Fail : TwoFactor() class RetrySocial : TwoFactor() } class Fail : SignIn() class RetrySocial : SignIn() } sealed class Link: SocialSignIn() { class Success : Link() sealed class TwoFactor : Link() { class Success : TwoFactor() class RetryTwoFactor : TwoFactor() class Fail : TwoFactor() class RetrySocial : TwoFactor() } class Fail : Link() class RetrySocial : Link() } sealed class Register : SocialSignIn() { class Success : Register() class Fail : Register() class RetrySocial : Register() } class Error(val errorMessage: String) : SocialSignIn() }
SocialSignIn() } sealed class Register : SocialSignIn() { class Success : Register() class Fail : Register() class RetrySocial : Register() } sealed class SignIn: SocialSignIn() { class Success : SignIn() class Fail : SignIn() class RetrySocial : SignIn() } sealed class TwoFactor : SignIn() { class Success : TwoFactor() class RetryTwoFactor : TwoFactor() class Fail : TwoFactor() class RetrySocial : TwoFactor() } sealed class Link: SocialSignIn() { class Success : Link() class Fail : Link() class RetrySocial : Link() }