Run-time exception when attempting to print a Unicode character

后端 未结 2 490
执念已碎
执念已碎 2021-02-12 14:51

Char is the type for Unicode characters in Haskell, and String is simply [Char] (i.e. a list of Char items). Here is some sim

2条回答
  •  孤街浪徒
    2021-02-12 15:26

    I think this should count as a bug in GHC, but there is a workaround. The default encoding for all handles in a GHC program (except those opened in Binary mode) is just the encoding accepted by the console with no error handling. Fortunately you can add error handling with something like this.

    makeSafe h = do
      ce' <- hGetEncoding h
      case ce' of
        Nothing -> return ()
        Just ce -> mkTextEncoding ((takeWhile (/= '/') $ show ce) ++ "//TRANSLIT") >>=
          hSetEncoding h
    
    main = do
      mapM_ makeSafe [stdout, stdin, stderr]
      -- The rest of your main function.
    

提交回复
热议问题