7

I have a function like:

type App a = ExceptT AppError (ResourceT IO)

onEvent :: SDL.EventPayload -> App ()
onEvent event = do
  liftIO $ putStrLn "EVE!"
  case event of
    SDL.MouseMotionEvent dat -> do
      liftIO $ putStrLn "HELLO"
    SDL.KeyboardEvent kbe  -> liftIO $ putStrLn "WORLD"
    _ -> return ()

That is a callback being used in my app.

This function seemingly doesn't fire, Because none of the putStrLn's print to console.

But this function - with the slight modification does print everything to console:

onEvent :: SDL.EventPayload -> App ()
onEvent event = do
  liftIO $ putStrLn "EVE!"
  case event of
    SDL.MouseMotionEvent dat -> do
      liftIO $ print dat
    SDL.KeyboardEvent kbe  -> liftIO $ print kbe
    _ -> return ()

Why does the full evaluation of SDL.EventPayload cause the surrounding putStrLn's to work?

How can I make my function callback a bit more reliable?

5
  • 6
    Are you sure this isn't just a buffering issue? Try hPutStrLn stderr instead. Jul 26, 2016 at 7:13
  • @ThomasM.DuBuisson Yes it is just a buffering issue! now I feel kind of stupid... Jul 26, 2016 at 8:32
  • Bah, no feeling stupid. It's a fair question. Glad things are working. Jul 26, 2016 at 16:16
  • @ThomasM.DuBuisson That should probably be an answer. If you don't want to write it up, I'm happy to!
    – jkeuhlen
    Jul 26, 2016 at 20:49
  • @jkeuhlen Knock yourself out! Jul 26, 2016 at 20:51

1 Answer 1

1

As Thomas pointed out in the comments, this looks like a buffering problem. The System.IO package describes standard buffering behavior.

You have a couple of options on how to resolve this kind of issue. You can manually set the buffering mode in your program with:

hSetBuffering stdout NoBuffering

at the beginning of your function. This will turn off all buffering (you could also choose LineBuffering) and will print immediately to stdout.

You could also flush the buffer after every print:

SDL.MouseMotionEvent dat -> do
  liftIO $ putStrLn "HELLO"
  hFlush stdout

Or you can try to print directly to the stderr handle which has different default buffering rules:

SDL.MouseMotionEvent dat -> do
  liftIO $ hPutStrLn stderr "HELLO"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.