https://kotlinlang.org logo
Title
e

edwardwongtl

12/10/2018, 9:51 AM
What type should I be putting in
???
such that
toResult
won’t error? I’ve tried
Nothing
and it is not working
sealed class ApiResult<T>

data class Success<T>(
    val msg: String,
    val data: T
) : ApiResult<T>()

data class Failure(
    val error: String,
    val errMsg: String
) : ApiResult<???>()

fun toResult(): ApiResult<User> {
        return if (error == "0") Success(
            msg = msg, data = movies
        ) else Failure(error = error, errMsg = errMsg)
    }
Using
Nothing
got me error
Required: ApiResult<User>, Found: Failure
k

karelpeeters

12/10/2018, 9:53 AM
Unit
maybe?
m

marstran

12/10/2018, 9:53 AM
Make
T
covariant by marking it with
out
. Then
Nothing
should work.
1
So
sealed class ApiResult<out T>

data class Success<T>(
    val msg: String,
    val data: T
) : ApiResult<T>()

data class Failure(
    val error: String,
    val errMsg: String
) : ApiResult<Nothing>()
That will make
ApiResult<Nothing>
a subtype of
ApiResult<T>
for all
T
.
e

edwardwongtl

12/10/2018, 9:54 AM
out
trick works!! Thanks
m

marstran

12/10/2018, 9:55 AM
No problem
d

Dico

12/10/2018, 12:43 PM
Shouldn't you just remove the type parameter on ApiResult, and keep it on Success?