IntelliJ无法解析符号n

时间:2016-09-03 01:34:28

标签: scala intellij-idea

我有以下scala代码。 IntelliJ说“无法解析符号n”。我试过“文件|无效”技巧,但它没有解决问题。

  abstract class Nat {
    def isZero: Boolean

    def predecessor: Nat

    def successor: Nat = new Succ(this)

    def +(that: Nat): Nat

    def -(that: Nat): Nat
  }

  object Succ (n: Nat) extends Nat {
    def isZero: Boolean = false
    def predecessor: Nat = n
    def +(that: Nat) = new Succ(n + that)
    def -(that: Nat): Nat =  if (that.isZero) this else n - that.predecessor
  }

2 个答案:

答案 0 :(得分:1)

Scala中的对象无法获取参数。这不是有效的Scala代码。这也适用于特征。如果需要传递构造函数参数,则使用类。

答案 1 :(得分:1)

你不能拥有object Succ的参数。请改用class

class Succ (n: Nat) extends Nat {
  def isZero: Boolean = false
  def predecessor: Nat = n
  def +(that: Nat) = new Succ(n + that)
  def -(that: Nat): Nat =  if (that.isZero) this else n - that.predecessor
}