generics - Kotlin 无法通过方法引用推断 Function 的类型
问题描述
为什么在下面的示例中,使用mapTwo
Kotlin 无法推断a
?
fun <U> mapOne(f: (Int) -> U): U = TODO()
fun <U> mapTwo(f: Function<Int, U>): U = TODO()
fun <T> mapper(a: T): List<T> = TODO()
fun main() {
mapOne(::mapper)
mapTwo(::mapper) //won 't compile
}
解决方案
类型推断不是编译错误的原因。mapTwo<List<Int>>(::mapper)
由于具有不可分配给::mapper
的类型(T) -> List<T>
( ),因此也不会编译。KFunction1<T, List<T>>
Function<Int, U>
推荐阅读
- azure - 在 Azure 服务总线(pub-sub)中的主题下创建的订阅的侦听器(订阅客户端)的数量是否有任何限制?
- javascript - 我想在 Nodejs 中将一个值从一个文件导出到另一个文件
- matlab - 修改双线性插值matlab代码以放大图像
- c - AVR I/O 宏定义如何工作以允许访问寄存器?
- python - 如何处理 CV2(Cv2.imwrite) 中的错误?
- python - 如何仅比较包含相同列信息的两个文件的一部分?
- django - 在django中过滤对象时传递外键参数
- unit-testing - 我有一个 apache camel 测试用例,由于创建了不同的对象实例而失败。对象实例具有不同的 ID
- excel - 复制工作表 1 中的范围并粘贴到循环中下一张工作表的下一个空行
- python - 从子目录和子目录中导入时如何解决冲突的导入要求?