Scala 解释器找不到我的类?

发布于 2024-12-11 03:55:37 字数 978 浏览 0 评论 0原文

我正在尝试在解释器中加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-12-18 03:55:37

我认为您想要 :paste 的行为。 :load 的行为就像您在解释器中键入一样,即,它一旦找到右大括号就会进行解释。您可以通过将代码包装在某个对象中来模拟 :paste ,如下所示:

object Test {
  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)
 }
}

现在您可以在 :load Test.scalaimport 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:

object Test {
  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)
 }
}

Now you can use it as you want after :load Test.scala and import Test._

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文