johnsam - 4 months ago 35

Scala Question

I have the following scala code. IntelliJ says "Cannot resolve symbol n". I tried "File | Invalidate " trick but it didn't solve the issue.

`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

}

Answer

You cannot have parameters for an `object`

Succ. use `class`

instead.

```
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
}
```