matt tighe
08/29/2019, 6:07 PMinterface ErrorField
data class InputError(
val field: ErrorField
val message: String
)
// elsewhere
internal sealed class Field : ErrorField {
object MyField : Field()
}
// complains about exhaustiveness
return when (error.field) {
is Field.MyField -> {}
}
is there a way to get the behavior i’m looking for? that is, to have a generally defined type but have the exhaustiveness check be aware that the subtype is actually exhausted?trevjones
08/29/2019, 6:15 PMInputError.field
is of type ErrorField
which is what the when statement is considering when checking for exhaustiveness.
you might consider doing something like:
kotlin
sealed class ErrorField {
abstract class SomeBaseType: ErrorField()
abstract class OtherBaseType: ErrorField()
}
when(error.field) {
is ErrorField.SomeBaseType -> ...
is ErrorField.OtherBaseType -> ...
}
matt tighe
08/29/2019, 6:18 PMtrevjones
08/29/2019, 6:21 PMmatt tighe
08/29/2019, 6:24 PMsealed class ErrorField {
abstract class BaseType : ErrorField()
}
// in module
object MyField : ErrorField.BaseType()
// use case
return when (error.field) {
is MyField -> {}
}
would be exhaustive?trevjones
08/29/2019, 6:25 PMwhen(error.field) {
is ErrorField.BaseType -> {}
}
matt tighe
08/29/2019, 6:26 PMtrevjones
08/29/2019, 6:27 PMmatt tighe
08/29/2019, 6:27 PMtrevjones
08/29/2019, 6:29 PMErrorField
so rather than doing a when(input.field) { is someType -> doStuff() }
you would just add a function to the interface and call it. input.field.doStuff()
matt tighe
08/29/2019, 6:30 PM