Wildcards generic in Kotlin for parameter

2019-07-08 08:56发布

问题:

I have an abstract class using for listen api callback like below. ApiRs is a parent object that every API response object inherit from it

abstract class ApiCallback<in T : ApiRs> {

    open fun onSucceed(apiRsModel: T) {}

    open fun onFailed(code: Int,
                      message: String) {
    }
}

and i also write a function to check network status before request api, if network is not connected, then it will trigger onFailed

fun isNetworkAvailable(apiCallback: ApiCallback<ApiRs>?,
                           context: Context): Boolean{
        val connectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.activeNetworkInfo

        // device network unreachable
        if (networkInfo == null
                || !networkInfo.isConnected) {
            // callback with network error
            apiCallback?.onFailed(code, message)
            return false
        }

        return true
    }

the parameter type ApiCallback<ApiRs>? actually i want to write it like ApiCallback<? extends ApiRs>?, but Kotlin has no wildcard. If i replace the generic to ApiCallback<out ApiRs>?, it will show an error

Projection is conflicting with variance of the corresponding type parameter of ApiCallback

Is there any way to fix it? or just i have wrong concept of Kotlin generic?


2017/09/18 update - The right way is :

Method :

fun <T : ApiRs> isNetworkAvailable(apiCallback: ApiCallback<T>?,
                                       context: Context): Boolean {
        val connectivityManager = context
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connectivityManager.activeNetworkInfo

        // device network unreachable
        if (networkInfo == null
                || !networkInfo.isConnected) {
            // callback with network error
            callbackWithFailed(
                    apiCallback,
                    ApiError.API_ERROR_CODE_0,
                    context.getString(R.string.api_error_code_0))
            return false
        }

        return true
    }

回答1:

I understand that you want to accept apiCallback: ApiCallback<ApiRs>? in your method in a way that the generic type of ApiCallback can be ApiRs itself or any of its subtypes. Instead of wildcards, Kotlin has upper bounds. You can declare the function like this:

fun <T: ApiRs> isNetworkAvailable(apiCallback: ApiCallback<T>?,
                       context: Context)