Michal Klimczak
sealed interface GeneralFormError { object FieldRequired : GeneralFormError } sealed interface SpecificFormError : GeneralFormError { object PostCodeInvalid : SpecificFormError } fun GeneralFormError.message() : String = when(this) { GeneralFormError.FieldRequired -> "Field is required" else -> throw IllegalStateException("Subtype of GeneralFormError not handled") } fun SpecificFormError.message() : String = when(this) { SpecificFormError.PostCodeInvalid -> "Post code is invalid" is GeneralFormError -> (this as GeneralFormError).message() }
val postCodeError : GeneralFormError
FieldRequired
PostCodeInvalid
postCodeError.message()
ephemient
is SpecificFormError -> message() // this calls the SpecificFormError overload since it has been smart casted
GeneralFormError.message()
A modern programming language that makes developers happier.