英文:
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(最后一行是List<SomeClass>) }
然后Intellij告诉我:
类型推断失败:
fun <T> get
(
path: String,
params: MutableMap<String, Any>? = ...,
headers: MutableMap<String, String>? = ...,
resolver: ResponseResolver<T> /* = (HttpResponse) → T */
)
: HttpRequest<T>;
不能应用于
(
String,
MutableMap<String, Any>,
(HttpResponse) -> List<SomeClass>
)
我不确定是否在将闭包作为带有默认参数的某些函数的参数时有任何限制。
英文:
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.
答案1
得分: 1
Kotlin不知道mutableMapOf("key" to "value")
是什么。
请澄清它是参数还是标头。
是
get("/somePath", headers = mutableMapOf("key" to "value")){ httpResponse -> ......some code(最后一行是List<SomeClass>)
还是
get("/somePath", mutableMapOf<String, Any>("key" to "value")){ httpResponse -> ......some code(最后一行是List<SomeClass>)
英文:
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>)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论