簡體   English   中英

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")是什么。

明確是參數還是標題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM