【问题标题】:Scala: Int doesn't take parameters on a recursive callScala:Int 在递归调用中不接受参数
【发布时间】:2017-10-05 04:29:38
【问题描述】:

在我正在进行的小型 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 下划线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 参数的方法被拒绝。如果可以的话,请给我一个提示,这将对我有很大帮助。

【问题讨论】:

  • 重命名 guess 参数(或方法名称,所以它是不同的) - 参数是范围内的第一个 guess,因此编译器认为您正在尝试将其称为功能。
  • 嗯,这很尴尬 :-) 你想把你的评论移到一个答案上,以便我可以将其标记为正确吗?

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


【解决方案1】:

重命名guess 参数(或方法名称,因此有所不同)- 参数是作用域中的第一个guess,因此编译器认为您正在尝试将其作为函数调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2015-06-08
    • 2023-03-28
    • 2019-05-22
    • 1970-01-01
    • 2018-06-14
    • 2018-01-30
    相关资源
    最近更新 更多