Stephen Vinouze
08/02/2023, 4:48 PMsealed interface Vehicle
data class Car(val engine: Engine) : Vehicle
data class Bike(val numberOfGears: Int) : Vehicle
Let’s assume we have a whole feature around cars but a different one for bikes (different flow and screens, etc). We’d like split car and bike related files (along with their dto/domain such as the one above) into distinct modules. And the idea would be to have the sealed declaration into a core module where both car and bike modules would depend on. Yet sealed interfaces can’t live outside the package of its implementations. So we can’t go that way. How would you approach this problem?
I’ve got few alternatives but none satisfy me. For instance, we could:
• Use a simple interface. But we loose our type safety and exhaustivity with when cases 😔
• Force the package name where the sealed interface is declared to be the same as its implementations in other modules and trick the compiler by letting it think it’s under the same package 🤮
• Still go with interfaces but leveraging KSP by generating exhaustive cases to solve the lose of exhaustiveness?
Would really love your inputs on that one 🙂Stepan Churyukanov
08/02/2023, 9:29 PMsealed interface Vehicle
interface BaseCar: Vehicle
interface BaseBike: Vehicle
Bikes module:
sealed interface Bike: BaseBike
data class SportBike(val numberOfGears: Int): Bike
data class ElectricBike(val power: Float): Bike
Cars module:
sealed interface Car: BaseCar
data class SportCar(val engine: Engine): Car
This structure allows you to use some advantages of the sealed interfaces, but declare entities in the separate modules.
For sure, BaseCar/BaseBike are not sealed and you can declare Bike2: BaseBike
but you can take as a rule to have a single inheritor from each interface in from the base module over the app.Stephen Vinouze
08/03/2023, 6:47 AMStephen Vinouze
08/03/2023, 8:41 AMRobert Williams
08/03/2023, 3:58 PMwhen (vehicle) {
is BaseBike -> {}
is BaseCar -> {}
} //exhaustive
Robert Williams
08/03/2023, 3:59 PM