【发布时间】:2021-12-30 18:32:53
【问题描述】:
我在 intellij 中实现了一个玩具工作表。
abstract class Nat:
def isZero: Boolean
def predecessor: Nat
def successor: Nat
def + (that: Nat): Nat
def - (that: Nat): Nat
end Nat
object Zero extends Nat:
def isZero: Boolean = true
def predecessor: Nat = ???
def successor: Nat = Succ(this)
def + (that: Nat): Nat = that
def - (that: Nat): Nat = if that.isZero then this else ???
override def toString = "Zero"
end Zero
class Succ(n: Nat) extends Nat:
def isZero: Boolean = false
def predecessor: Nat = n
def successor: Nat = Succ(this)
def + (that: Nat): Nat = Succ(n + that)
def - (that: Nat): Nat = if that.isZero then this else n - that.predecessor
override def toString = s"Succ($n)"
end Succ
val two = Succ(Succ(Zero)) // : Succ =
val one = Succ(Zero)
two + one
two - one
//one - two
到目前为止,其他工作表的评估都很好,但是当我评估这个时,我遇到了一些错误,如下所示:
// defined class Nat
4 | def successor: Nat = Succ(this)
| ^^^^
| Not found: Succ
// defined class Succ
1 |val two = Succ(Succ(Zero))
| ^^^^
| Not found: Zero
1 |val one = Succ(Zero)
| ^^^^
| Not found: Zero
1 |two + one
|^^^
|Not found: two
1 |two - one
|^^^
|Not found: two
如何解决未找到的错误?
【问题讨论】:
-
工作表逐行评估,在该行,
Succ不存在。您能做的最好的事情就是在普通项目中编写该代码,然后在工作表中导入这些类,或者只使用 main 方法。 -
或将所有内容包装在一个对象中
-
试试这个:调出 Worksheet Settings(点击左上角的小扳手图标)并将 Run type: 从 REPL 更改为清楚的。看看有没有帮助。
标签: scala intellij-idea