electrolobzik
01/26/2024, 9:38 AMsealed interface Label {
...
data object NoConnection: Label
data object TechnicalError: Label
}
All this stores should contain some subset of children (in this example NoConnection
and TechnicalError
). So I would like to do something like this:
sealed interface BaseErrorHandlingLabel {
data object NoConnection: BaseErrorHandlingLabel
data object TechnicalError: BaseErrorHandlingLabel
}
sealed interface Label extends BaseErrorHandlingLabel {
data object CustomA: Label
data object CustomB: Label
}
and get NoConnection
and TechnicalError
included in each inherited interface.
I didn’t find any way to to that in Kotlin right now and have to duplicate this logic in each sealed class/interface. Am I missing something?hho
01/26/2024, 9:48 AMsealed
to remove the possibility of extending?electrolobzik
01/26/2024, 10:03 AMsealed
is to make it possible to do exhaustive when
check with compile-time errors. Extending doesn’t violate this idea. If you make when
for the BaseErrorHandlingLabel
you will have only 2 possible values. And if you make when
for the Label
you will have 2 options from the base class + children. It is similar to class inheritance, when you don’t see additional functions/fields if you use reference to a parent class, but if you use the child it is possible to use both parent’s and his own members.Klitos Kyriacou
01/26/2024, 10:39 AMNoConnection
and TechnicalError
both implement Label
which is the sub-interface. So if you created another sub-interface, say sealed interface AnotherLabel : BaseErrorHandlingLabel
you wouldn't be able to use NoConnection
and TechnicalError
because they are Label
, and not AnotherLabel
.electrolobzik
01/26/2024, 10:44 AMNo Connection
and TechnicalError
. In real life this 2 interfaces are not in the same package. I can have many features in different packages and modules and I would like to use same set of pre-configured items in them, defined in one core module.Okizuys
01/26/2024, 11:18 AMsealed
classes/interfaces, it is not possible to extend from different package, seeKlitos Kyriacou
01/26/2024, 11:41 AMBaseErrorHandlingLabel <-- NoConnection
<-- TechnicalError
<-- Label <-- CustomA
<-- CustomB
So an exhaustive when
for Label
would require only CustomA
and CustomB
. No instance of Label
can ever be NoConnection
or TechnicalError
because they're not Label
.
It seems what you are looking for is a way to say, "every sub-interface of this interface includes the following classes as though they were defined in the subinterface itself".electrolobzik
01/26/2024, 1:35 PMDavid Kubecka
01/26/2024, 9:51 PMelectrolobzik
01/27/2024, 8:05 AMDavid Kubecka
01/27/2024, 9:50 AM