开发者

Scala REPL exiting automatically

开发者 https://www.devze.com 2023-04-02 14:44 出处:网络
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 REP

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 开发者_如何学Cterminal. 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


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).


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) }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消