我有一个小程序,它定义一个帐户,一个提款功能并尝试从中提款。但是,它不能编译到期并引发以下错误:
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"
答案 0 :(得分:6)
{-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
函数。
注释的确在语义上没有任何作用,但是您可以考虑将其替换为空格。