Pass A Sealed Class As Function Argument Android
I have a sealed class which represents the Retrofit Response of my API. sealed class NetworkResponse { data class Success(val
Solution 1:
Since you care only about ApiError
, NetworkError
and UnknownError
, which all derive from NetworkResponse
but don't use the first generic type, you can specify that you don't care about it using *
(Actually, depending on what you want to do with mError
, you can replace U
with *
too - that is the case in the code below, but I introduced U
just in case). In that case, you should accept a NetworkReponse
:
fun <U : Any> networkErrorHanlder(mError: NetworkResponse<*, U>) {
when(mError) {
is NetworkResponse.ApiError ->
print("Api stuff: ${mError.body}")
is NetworkResponse.NetworkError ->
print ("Network stuff: ${mError.error}")
is NetworkResponse.UnknownError ->
print("Unknown: ${mError.error}")
else -> print("It must've been a Success...")
}
}
Post a Comment for "Pass A Sealed Class As Function Argument Android"