Frege putStr刷新行为与Haskell或Java不同

时间:2015-07-31 09:14:50

标签: frege

假设您使用putStrgetLine的组合提示用户输入:

main = do
    putStrLn "A line with line termination" -- printed correctly
    putStr   "A line without line termination, e.g. to prompt for input: " -- NOT printed
    line <-  getLine
    putStrLn ("You entered: " ++ line)

与Haskell相比,Frege不打印第二行(使用putStr而不是putStrLn)。这种缺失冲洗的行为是否意图?

如果Frege偏离Haskell行为,我会认为它可以模仿Java的行为。概念上类似的例子:

public static void main(String[] args) {
    System.out.println("A line with line termination");
    System.out.print("A line without line termination, e.g. to prompt for input: ");
    String line = new java.util.Scanner(System.in).nextLine();
    System.out.println("You entered: " + line);
}

然而,这与Haskell变体的行为类似,即System.out.print立即刷新。

提前感谢您的任何反馈!

PS:可以使用最新的Eclipse-Plugin以及IntelliJ / Gradle重现(mis?)行为。

1 个答案:

答案 0 :(得分:5)

您的Java代码使用System.out,它是一个PrintStream。 Frege代码使用PrintWriter。

这两个类在刷新方面的工作方式略有不同。来自PrintWriter的文档:

  

与{@link PrintStream}类不同,如果启用了自动刷新   只有在调用println,printf或format方法之一时才会执行它。

因此,对于您的弗雷格代码,您必须在stdout.flush之后添加print以使其立即显示。

在这方面,请随意提出将Frege与Haskell行为对齐的请求的问题。 (我们可以保留print,但putStr会自动添加flush。)