gpt4 book ai didi

scala - Scala 中令人费解的类型不匹配 foldRight

转载 作者:行者123 更新时间:2023-12-04 23:05:26 26 4
gpt4 key购买 nike

有人可以告诉我这个函数定义有什么问题吗?

def incr[Int](l: List[Int]): List[Int] = 
l.foldRight(List[Int]())((x,z) => (x+1) :: z)

Scala 编译器提示传递给 foldRight 的函数体中的类型不匹配:
<console>:8: error: type mismatch;
found : Int(1)
required: String
l.foldRight(List[Int]())((x,z) => (x+1) :: z)
^

这里有什么问题?

最佳答案

def incr[Int]您已经定义了一个名为 Int 的任意类型,它会覆盖现有的。摆脱[Int]输入参数,它工作正常,或者使用不同的名称,如 T .

关于scala - Scala 中令人费解的类型不匹配 foldRight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13465866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com