我有一个小程序来定义帐户,提取功能并尝试从该帐户中提取。但是,它不会编译到应付并丢弃以下错误:
Couldn't match expected type ‘(STM a0 -> IO a0)
-> STM () -> IO ()’
with actual type ‘IO ()’
似乎编译器无法识别从STM到IO的转换。任何指针都会很棒。
import System.IO
import Control.Concurrent.STM
type Account = TVar Int
withdraw :: Account -> Int -> STM ()
withdraw acc amount = do
bal <- readTVar acc
writeTVar acc (bal - amount)
good :: Account -> IO ()
good acc = do
hPutStr stdout "Withdrawing..."
{-hi-}atomically{-/hi-} (withdraw acc 10)
main = do
acc <- atomically (newTVar 200)
good acc
hPutStr stdout "nDone!n"
{-hi-}
和 {-/hi-}
注释结果"缩进" automically
,因此,您写了 hPutStr stdout "Withdrawing..." atomically (withdraw acc 10)
。例如,如果您写:
good :: Account -> IO ()
good acc = do
hPutStr stdout "Withdrawing..."
{-hi-} atomically (withdraw acc 10)
它可以正常工作,因为"噪声"({-hi-}
注释(不会导致atomically
函数。
该评论确实没有语义上没有效果,但是您可以认为它被空间取代。