Iheme Tobechukwu
03/27/2025, 7:09 AMsealed interface NetworkState<out T> {
object Loading: NetworkState<Nothing>
data class Success<T> (val data: T): NetworkState<T>
data class Error(val message: String?): NetworkState<Nothing>
}
I use it here:
sealed class AuthState<out T> : NetworkState<T>{
object Completed: AuthState<Nothing>()
}
And whenever i created an instance of the NetworkState, i can also check for conditions of the AuthState in my when
block which is fine. Now i want to introduce another sealed class:
sealed class TableState<out T>: NetworkState<T>{
}
But i am having trouble using this as i want it to have other child heirachies whilst also inheriting the ones from NetworkState. How do I clearly differentiate from an AuthState implementation and a TableState implementationJoffrey
03/27/2025, 7:51 AMYoussef Shoaib [MOD]
03/27/2025, 7:59 AMNetworkState: TableState
and not the other way around.Michael de Kaste
03/27/2025, 8:34 AMMichael de Kaste
03/27/2025, 8:39 AMIheme Tobechukwu
03/27/2025, 8:40 AMMichael de Kaste
03/27/2025, 8:57 AMMichael de Kaste
03/27/2025, 8:57 AMwhen(state){
is AuthState.Completed -> println("Completed")
is AuthState.Success -> //This does not compile because Success is not of AuthState
}
Michael de Kaste
03/27/2025, 8:58 AMYoussef Shoaib [MOD]
03/27/2025, 9:03 AMsealed interface NetworkState<out T>: AuthState<T>, TableState<T> { ... }
sealed interface AuthState<out T> { ... }
sealed interface TableState<out T> { ... }
Youssef Shoaib [MOD]
03/27/2025, 9:03 AMXState
you add needs to be added as a super interface of NetworkState
Iheme Tobechukwu
03/27/2025, 9:19 AMYoussef Shoaib [MOD]
03/27/2025, 9:19 AMJoffrey
03/27/2025, 9:43 AMwhen
. What do you do, business-wise, in the when statements? Maybe you can encapsulate the behaviour of the whole when
in a single function (or a few) that you can define on the parent interface, and then call on any type of instance without thinking about special cases. The additional functionality provided by the extra subclasses of some hierarchies could translate to new functions only available in those subhierachies.Iheme Tobechukwu
03/27/2025, 9:50 AM