我发现添加类型提示对调试很有用,但我不知道如何使用<-
on 和 IO 操作的结果来做到这一点
action :: IO ()
foo :: String --doesnt't compile
foo <- getLine
您不能这样做,因为 <- 不是声明。您可以:
action :: IO ()
action = do
foo <- getLine :: IO String
...
或者,使用{-# LANGUAGE ScopedTypeVariables #-}
:
action :: IO ()
action = do
foo :: String <- getLine
...
有了-XScopedTypeVariables
,你就可以拥有(foo :: String) <- getLine
。
为了完整起见,我想添加
action :: IO ()
action = do
foo <- getLine
let bar :: String
bar = foo
print bar
这很笨重,但如果您发现自己被困在 IO monad 中,这可能会很有用,如果您正在编写 GUI,就会发生这种情况。