Scala REPL 自动退出

发布于 2024-12-02 23:07:48 字数 339 浏览 0 评论 0原文

Scala REPL 的行为很奇怪,或者这可能是预期的行为。当我创建 MainFrame 对象并将其可见性设置为 true 时,会显示一个窗口。但是,如果我关闭窗口,Scala REPL 将退出到终端。示例会话:

 ~$ scala
 scala> import swing._
 scala> val frame = new MainFrame()
 scala> frame.visible = true
 ~$                             //when I close the window

我在 kubuntu 上使用 scala 2.9.1

Scala REPL is behaving oddly or perhaps this is the expected behavior. When I create a MainFrame object and set its visibility true, a window is displayed. However, If I close the window the Scala REPL exits to the terminal. Sample session:

 ~$ scala
 scala> import swing._
 scala> val frame = new MainFrame()
 scala> frame.visible = true
 ~$                             //when I close the window

I am using scala 2.9.1 on kubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

潜移默化 2024-12-09 23:07:48

它是 MainFrame 类本身,加上 System.exit 的不太 OO 行为。

这是 MainFrame 的完整源代码:

class MainFrame extends Frame {
  override def closeOperation() { sys.exit(0) }
}

看一下,很明显,当窗口关闭时,System.exit 被调用,JVM 将退出。

如果您只是在尝试时发现了这一点,那么解决方法就是不要这样做!如果您想在 REPL 中使用框架,那么您可以覆盖 closeOperation 以不退出 JVM - 或者仅使用 Frame (因为 MainFrame 的唯一附加功能是 JVM 退出行为)。

It's the MainFrame class itself, coupled with the not-very-OO behaviour of System.exit.

This is the entire source of MainFrame:

class MainFrame extends Frame {
  override def closeOperation() { sys.exit(0) }
}

Looking at that, it's pretty clear that when the window is closed, System.exit is called and the JVM will quit.

If you were just experimenting when you found this, the workaround is to just not do this! If you want to use a frame in the REPL, then you can either override closeOperation to not exit the JVM - or just use a Frame (since the only additional functionality with MainFrame is the JVM exit behaviour).

猫烠⑼条掵仅有一顆心 2024-12-09 23:07:48

正如文档中所述:

关闭框架并在关闭时退出应用程序。

(即,它关闭运行 REPL 的 JVM。)

要防止这种行为,您可以简单地使用 Frame 相反,或者重写 closeOperation 方法。

这是 MainFrame.scala 的源代码供参考:

/**
 * A frame that can be used for main application windows. Shuts down the
 * framework and quits the application when closed.
 */
class MainFrame extends Frame {
  override def closeOperation() { sys.exit(0) }
}

As it says in the documentation:

Shuts down the framework and quits the application when closed.

(I.e., it shuts down the JVM which the REPL runs in.)

To prevent this behavior you could either simply use a Frame instead, or override the closeOperation method.

Here is the source for MainFrame.scala for reference:

/**
 * A frame that can be used for main application windows. Shuts down the
 * framework and quits the application when closed.
 */
class MainFrame extends Frame {
  override def closeOperation() { sys.exit(0) }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文