How do you print an error from a child thread in Haskell?

Given the following code:

module Main where

import Control.Concurrent

main :: IO ()
main = forkIO errorPrinter >> threadDelay 1000000 >> print "Finished"

errorPrinter = error "You can't see me!"

I would expect that after running this code, I would see the following:

"You can't see me"
"Finished!"

Printed on the console. However, "You Can't See Me" is never printed.

I understand that using an error is not a good idea in production code, but I use it in development, and I find that errors are not printed to the console - even those related to dependencies.

I am sure this problem has a simple solution. I cannot be the first person to have this problem, but I cannot find the answer anywhere.

Any help would be greatly appreciated!

+4
source share
1 answer

; " !":

% runhaskell test.hs
test.hs: You can't see me!
CallStack (from HasCallStack):
  error, called at test.hs:8:16 in main:Main
"Finished"

GHC 7.6 ( GHC, ); , , .

:

import System.IO
errorPrinter = hPutStrLn stderr "You can't see me!"

, , .

+1

Source: https://habr.com/ru/post/1680894/


All Articles