2016-09-03 89 views
0

我有以下的scala代码。 IntelliJ说“无法解析符号n”。我试过“文件|无效”技巧,但它没有解决问题。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 
    } 

回答

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 
} 
+1

啊。我会牢记这一点。 – johnsam

+0

@johnsam与你的课程祝你好运;) –

1

Scala中的对象不能带参数。这不是有效的Scala代码。这也适用于特征。如果你需要传递构造函数参数,然后使用一个类。

+0

对于性状,这将很快改变([SIP](http://docs.scala-lang.org/sips/pending/trait-parameters.html)和[在斑点狗PR(HTTPS: //github.com/lampepfl/dotty/pull/639))。 – Alec