Scala 解释器找不到我的类?
我正在尝试在解释器中加载 Scala 文件:
trait MyOrdered {
def <(that: MyInt):Boolean = compare(that) < 0
def >(that: MyInt):Boolean = compare(that) > 0
def <=(that: MyInt):Boolean = compare(that) < 0 || this == that
def >=(that: MyInt):Boolean = compare(that) > 0 || this == that
def compare(that: MyInt): Int
}
class MyInt(val value: Int) extends MyOrdered {
def compare(that: MyInt) =
if (this.value < that.value) -1
else if (this.value == that.value) 0
else 1
}
object App extends Application{
val a = new MyInt(2)
val b = new MyInt(4)
println(a < b)
println(a > b)
}
但我收到一个愚蠢的错误:
Loading traits.scala...
<console>:8: error: not found: type MyInt
def <(that: MyInt):Boolean = compare(that) < 0
^
<console>:12: error: not found: type MyInt
def compare(that: MyInt): Int
如何让解释器知道稍后定义的 MyInt 类?
I'm trying to load a Scala file inside the interpreter:
trait MyOrdered {
def <(that: MyInt):Boolean = compare(that) < 0
def >(that: MyInt):Boolean = compare(that) > 0
def <=(that: MyInt):Boolean = compare(that) < 0 || this == that
def >=(that: MyInt):Boolean = compare(that) > 0 || this == that
def compare(that: MyInt): Int
}
class MyInt(val value: Int) extends MyOrdered {
def compare(that: MyInt) =
if (this.value < that.value) -1
else if (this.value == that.value) 0
else 1
}
object App extends Application{
val a = new MyInt(2)
val b = new MyInt(4)
println(a < b)
println(a > b)
}
But I receive a silly error:
Loading traits.scala...
<console>:8: error: not found: type MyInt
def <(that: MyInt):Boolean = compare(that) < 0
^
<console>:12: error: not found: type MyInt
def compare(that: MyInt): Int
How can I make the interpreter know the MyInt
class, which is defined down the road?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想要
:paste
的行为。:load
的行为就像您在解释器中键入一样,即,它一旦找到右大括号就会进行解释。您可以通过将代码包装在某个对象中来模拟:paste
,如下所示:现在您可以在
:load Test.scala
和import Test 之后使用它._
I think you want the behavior of
:paste
.:load
behaves as if you are typing in the interpreter i.e., it interprets as soon as it finds the closing braces. You can emulate the:paste
by wrapping your code in some object like this:Now you can use it as you want after
:load Test.scala
andimport Test._