简体   繁体   English

Kotlin 中默认参数后面的闭包作为参数

[英]The closure as parameter behind the default parameter in Kotlin

I have a function我有一个功能

fun <T> get(path: String, params: MutableMap<String, Any>? = null, headers: MutableMap<String, String>? = null, resolver: ResponseResolver<T>): HttpRequest<T>

which ResponseResolver is a type alias其中 ResponseResolver 是类型别名

typealias ResponseResolver<T> = (HttpResponse) -> T

When i invoke the get method like below:当我调用如下 get 方法时:

get("/somePath", mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)

Then the Intellij tells me that然后 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>
)

智能提示

I'm not sure if there is any strictions in applying the closure as the argument of some functions with default parameters.我不确定将闭包用作某些具有默认参数的函数的参数是否有任何限制。

Kotlin don't know exactly what mutableMapOf("key" to "value") is. Kotlin 并不确切知道mutableMapOf("key" to "value")是什么。

Clarify whether it's params or headers明确是参数还是标题

get("/somePath", headers = mutableMapOf("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)

or或者

get("/somePath", mutableMapOf<String, Any>("key" to "value")){ httpResponse -> ......some code(Last line is a List<SomeClass>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM