闭包作为 Kotlin 中默认参数后面的参数
The closure as parameter behind the default parameter in Kotlin
我有一个功能
fun <T> get(path: String, params: MutableMap<String, Any>? = null, headers: MutableMap<String, String>? = null, resolver: ResponseResolver<T>): HttpRequest<T>
哪个 ResponseResolver 是类型别名
typealias ResponseResolver<T> = (HttpResponse) -> T
当我像下面这样调用 get 方法时:
get("/somePath", mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)
然后 Intellij 告诉我
Type inference failed:
fun <T> get
(
path: String,
params: MutableMap<String, Any>? = ...,
headers: MutableMap<String, String>? = ...,
resolver: ResponseResolver<T> /* = (HttpResponse) → T */
)
: HttpRequest<T>
cannot be applied to
(
String,
MutableMap<String, Any>,
(HttpResponse) → List<SomeClass>
)
我不确定将闭包用作某些具有默认参数的函数的参数是否有任何限制。
Kotlin 不知道 mutableMapOf("key" to "value")
是什么。
弄清楚是params还是headers
get("/somePath", headers = mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)
或
get("/somePath", mutableMapOf<String, Any>("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)
我有一个功能
fun <T> get(path: String, params: MutableMap<String, Any>? = null, headers: MutableMap<String, String>? = null, resolver: ResponseResolver<T>): HttpRequest<T>
哪个 ResponseResolver 是类型别名
typealias ResponseResolver<T> = (HttpResponse) -> T
当我像下面这样调用 get 方法时:
get("/somePath", mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)
然后 Intellij 告诉我
Type inference failed:
fun <T> get
(
path: String,
params: MutableMap<String, Any>? = ...,
headers: MutableMap<String, String>? = ...,
resolver: ResponseResolver<T> /* = (HttpResponse) → T */
)
: HttpRequest<T>
cannot be applied to
(
String,
MutableMap<String, Any>,
(HttpResponse) → List<SomeClass>
)
我不确定将闭包用作某些具有默认参数的函数的参数是否有任何限制。
Kotlin 不知道 mutableMapOf("key" to "value")
是什么。
弄清楚是params还是headers
get("/somePath", headers = mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)
或
get("/somePath", mutableMapOf<String, Any>("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)