在声明部分函数时,获取“扩展函数的缺少参数类型”错误

问题描述 投票:0回答:1

我在声明部分函数的类型推断方面遇到了一些问题。我尝试了下面的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 editor没有显示任何警告/错误

直到我运行测试类并且存在一些编译错误:missing parameter type for expanded function

sum2defsum2valScala shell without given function type工作正常

我认为Scala编译器应该能够推断出sum2defsum2val的类型而不说明函数类型Int => Int

我的问题是:

  1. 为什么我的intellij编辑器在编译代码之前没有向我显示错误/警告?我的代码在scala语法中是否有效?如果它无效,如何设置我的intellij向我显示错误?
  2. 为什么我在intellij中使用的代码不能编译但在scala shell中工作正常?
  3. valdef在我的intelliJ中表现不同? def显示函数推断类型,而val没有。

谢谢

scala intellij-idea type-inference scalatest
1个回答
0
投票

您的2个问题的答案:

1:见例如:Scala unexpectedly not being able to ascertain type for expanded functionWhy do I get a "missing parameter for expanded function" in one case and not the other?

3:我相信这确实是IntellIJ的一个实现选择,我赞同它。我不想为我定义的每个String或Int值提供类型提示。一世

© www.soinside.com 2019 - 2024. All rights reserved.