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

问题描述 投票:0回答:1

我有一个功能

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

Intellij Hint

我不确定在将闭包用作具有默认参数的某些函数的参数时是否有任何严格限制。

kotlin lambda parameters closures type-inference
1个回答
0
投票

Kotlin并不完全知道mutableMapOf("key" to "value")是什么。

© www.soinside.com 2019 - 2024. All rights reserved.