如何让我的 Scala 程序退出并返回 REPL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15693232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make my scala program quit and return to the REPL?
提问by Chris
So I made this scala file and it works great when I load it into the REPL. What I want to do though is when the user inputs "Q", it exits the program and returns to the REPL. I already have readLine set up with a case match that says:
所以我制作了这个 scala 文件,当我将它加载到 REPL 中时它工作得很好。我想做的是当用户输入“Q”时,它退出程序并返回到 REPL。我已经用大小写匹配设置了 readLine ,上面写着:
case "Q" =>
I just don't know what to put after it to make the program quit.
我只是不知道在它后面放什么才能退出程序。
Thanks
谢谢
回答by Randall Schulz
You can use System.exit(0)providedyou fork a new console / REPL. If you run via SBT, then fork in console := truewill accomplish that. If you're launching a REPL from within your code and using runin instead of console, then you'd want fork in run.
您可以使用System.exit(0)提供你fork一个新的控制台/ REPL。如果您通过 SBT 运行,那么fork in console := true将实现这一点。如果您从代码中启动 REPL 并使用runin 而不是console,那么您需要fork in run.
If you want to run a stand-alone REPL, then start your program and eventually have it exit back to the REPL, then you'll need to simply stop your read loop and return out of the entry-point method you called to start it up.
如果你想运行一个独立的 REPL,然后启动你的程序并最终让它返回到 REPL,那么你需要简单地停止你的读取循环并返回你调用的入口点方法来启动它向上。
Given how little code you included, it's hard to say much more than this.
鉴于您包含的代码很少,很难说更多。

