关于声明部分函数的类型推断,我遇到了一些问题。我尝试了下面的Scalatest代码:
class FunSpecTest extends FunSpec {
describe("Partial Function Test") {
def sum1(a: Int, b: Int): Int = a + b
def sum2 = (a: Int, b: Int) => a + b // type-inference hints shown in Intellij
val sum3 = (a: Int, b: Int) => a + b
describe("Partial Function with 2 params") {
it("add 2") {
val sum1val: Int => Int = sum1(_, 2)
assertResult(3)(sum1val(1))
def sum2def = sum2(_, 2)// compilation error, but type-inference hints shown in Intellij
val sum2val = sum2(_, 2)// compilation error
assertResult(3)(sum2def(1))
assertResult(3)(sum2val(1))
val sum3val: Int => Int = sum3(_, 2)
assertResult(3)(sum3val(1))
val sum3valWithoutType= sum3(_, 2) // compilation error
assertResult(3)(sum3valWithoutType(1))
}
}
}
}在我的intelliJ编辑器中没有显示任何警告/错误
直到我运行这个测试类,并且有一些编译错误:扩展函数的缺失参数类型
但是sum2def和sum2val在没有给定函数类型的Scala shell中工作得很好
我认为Scala编译器应该能够推断sum2def和sum2val的类型,而不需要声明函数类型Int => Int。
我的问题是:
val和def在我的intelliJ?def中的行为不同,它显示函数推断类型,而val没有。谢谢
发布于 2019-04-01 07:39:15
你的两个问题的答案:
1:例如见:Scala出乎意料地无法确定扩展函数的类型和为什么在一种情况下得到“扩展函数缺少的参数”而不是另一种情况?
他说:我相信这确实是IntellIJ的一个实现选择,我对此表示赞同。我不希望为我定义的每个字符串或Int值提供类型提示。我
https://stackoverflow.com/questions/55448732
复制相似问题