Strange issue with SBT, println, and scala console application

懵懂的女人 提交于 2020-06-14 07:55:06

问题


When I run my scala code (I'm using SBT), the prompt is displayed after I enter some text as shown here:

C:\... > sbt run
[info] Loading project definition [...]
[info] Set current project to [...]
Running com[...]
test
>>





exit
>> >> >> >> >> >> [success] Total time[...]

It seems like it's stacking up the print() statements and only displaying them when it runs a different command.

If I use println() it works as it should (except that I don't want a newline)

The code:

...
  def main(args:Array[String]) {
    var endSession:Boolean = false
    var cmd = ""
    def acceptInput:Any = {
      print(">> ")
      cmd = Console.readLine
      if (cmd != "exit") {
        if (cmd != "") runCommand(cmd)
        acceptInput
      }
    }

    acceptInput
  }
...

What's going on here?


回答1:


Output from print (and println) can be buffered. Scala sends output through java.io.PrintStream, which suggests that it will only auto-flush on newline, and then only if you ask. It might be OS dependent, though, since my print appears immediately.

If you add Console.out.flush after each print, you'll empty out the buffer to the screen (on any OS).



来源:https://stackoverflow.com/questions/16843429/strange-issue-with-sbt-println-and-scala-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!