[Git][ghc/ghc][master] Update interact docs to explain about buffering
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: c6cd2da1 by Jappie Klooster at 2025-07-17T06:06:20-04:00 Update interact docs to explain about buffering We need to tell the user to set to the appropriate buffer format. Otherwise, this function may get randomly stuck, or just behave confusingly. issue: https://gitlab.haskell.org/ghc/ghc/-/issues/26131 NB, I'm running this with cabal *NOT* ghci. ghci messes with buffering anyway. ```haskell interaction :: String -> String interaction "jappie" = "hi" interaction "jakob" = "hello" interaction x = "unkown input: " <> x main :: IO () main = interact interaction ``` so in my input (prefixed by `>`) I get: ```
jappie unkown input: jappie
we confirmed later this was due to lack of \n matching.
Anyway movnig on to more unexpected stuff:
```haskell
main :: IO ()
main = do
interact (concatMap interaction . lines)
get's stuck forever. actually `^D` (ctrl+d) unstucks it and runs all input as expected. for example you can get: ```
sdfkds fakdsf unkown input: sdfkdsunkown input: fakdsf
This program works!
```haskell
interaction :: String -> String
interaction "jappie" = "hi \n"
interaction "jakob" = "hello \n"
interaction x = "unkown input: " <> x <> "\n"
main :: IO ()
main = do
interact (concatMap interaction . lines)
the reason is that linebuffering is set for both in and output by default. so lines eats the input lines, and all the \n postfixes make sure the buffer is put out. - - - - - 1 changed file: - libraries/ghc-internal/src/GHC/Internal/System/IO.hs Changes: ===================================== libraries/ghc-internal/src/GHC/Internal/System/IO.hs ===================================== @@ -425,6 +425,21 @@ getContents' = hGetContents' stdin -- -- This operation may fail with the same errors as 'getContents' and 'putStr'. -- +-- If it doesn't produce output the buffering settings may not be +-- correct, use ^D (ctrl+D) to close stdin which forces +-- the buffer to be consumed. +-- +-- You may wish to set the buffering style appropriate to your program's +-- needs before using this function, for example: +-- +-- @ +-- main :: IO () +-- main = do +-- hSetBuffering stdin LineBuffering +-- hSetBuffering stdout NoBuffering +-- interact (concatMap (\str -> str ++ str) . L.lines) +-- @ +-- -- ==== __Examples__ -- -- >>> interact (\str -> str ++ str) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c6cd2da1343dd7f7cb8952ede849b7e0... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c6cd2da1343dd7f7cb8952ede849b7e0... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Marge Bot (@marge-bot)