在`Pair <K,V>`中指定<K,V>类型

yco*_*omp 4 kotlin

我怎样才能摆脱这些as String演员?

我想要做的是改变fun定义,但我不知道该怎么做......包含String在那里

 parametersOf("appKey" to "asdas3334", "token" to "433432")

/**
 * Returns a new [Parameters] with the specified contents, given as a list of pairs
 * where the first component is the key and the second is the value.
 */
fun <K, V> parametersOf(vararg pairs: Pair<K, V>): Parameters {

    val p = Parameters(pairs.size)

    for ((key, value) in pairs)
        p.put(key as String, value as String)

    return p
}
Run Code Online (Sandbox Code Playgroud)

Luk*_*itz 5

只需摆脱通用定义并使用Pair<String, String>:

fun parametersOf(vararg pairs: Pair<String, String>): Parameters {

    val p = Parameters(pairs.size)

    for ((key, value) in pairs)
        p.put(key, value) 

    return p
}
Run Code Online (Sandbox Code Playgroud)