简体   繁体   中英

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

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

When i invoke the get method like below:

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

Then the Intellij tells me that

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.

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>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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