Scala:Int不会在递归调用上获取参数

时间:2017-05-06 23:00:18

标签: scala recursion sbt pattern-matching string-operations

我在我正在进行的小型Scala练习中遇到了一个奇怪的编译错误。

我有这种方法应该继续询问用户输入,直到提供正确的答案。唉,我偶然发现我的模式匹配中的第一个案例:

  override def guess(guess: Int):Unit = {
    val guessIndex = binary(array, guess)
    guessIndex match {
      case -1 => {
         val nextAttempt = StdIn.readLine(s"Please be attentive $guess is outside the search range" 
              +" (0 to $upperBound). Try again: \n");
         val a = validateType[Int](nextAttempt)
         guess(a)
      }
    }
  }

IDE使用错误&#34强调guess(a); Int不接受参数"。从控制台运行sbt compile确认此错误:

> compile
[info] Compiling 2 Scala sources to /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/target/scala-2.12/classes...
[error] /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/src/main/scala/ca/vgorcinschi/algorithms1_4_34/hotandcold/HotAndColdImpl.scala:23: Int does not take parameters
[error]          guess(a)
[error]               ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed 6-May-2017 6:47:58 PM

对于相同的错误消息,几乎没有不同的Stackoverflow票证,但它们适用于不同的场景。在我看来,它看起来像一个采用Int参数的方法被拒绝。如果你能给我一个提示,这对我有很大的帮助。

1 个答案:

答案 0 :(得分:3)

重命名guess参数(或方法名称,因此它有所不同) - 参数是范围内的第一个guess,因此编译器认为您正在尝试将其称为功能。