putStrLn doesn't print to console

萝らか妹 提交于 2019-12-07 06:40:05

问题


I am experimenting with wxHaskell. I wasn't able to run the app under ghci, so I have to use application to test it. I wanted to test the program with println debugging. However, it seems that putStrLn doesn't work in GUI:

{-# LANGUAGE Haskell2010 #-}

module Main where

import Graphics.UI.WX

drawUI dc view = do 
  circle dc (point 10 10) 5 [penKind := PenSolid, color := red]
  putStrLn "painted"  

helloGui :: IO ()
helloGui = do
  f <- frame [
    text := "Example", 
    resizeable := False, 
    bgcolor := white,
    layout := space 400 300,
    on paint := drawUI]
    return ()

main :: IO ()
main = do
  putStrLn "Started"
  start helloGui

If I comment out start helloGui, everything is printed well. However, if I return it, nothing is printed but the window is displayed. What's wrong here?


回答1:


This is probably output buffering; the output is not written until the program exits.

Either flush explicitly:

  putStrLn "Started"
  hFlush stdout

Or turn on line buffering:

  hSetBuffering stdout LineBuffering -- or even NoBuffering
  putStrLn "Started"


来源:https://stackoverflow.com/questions/12435794/putstrln-doesnt-print-to-console

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