繁体   English   中英

Haskell中的软件事务存储器:无法将预期的STM a0类型与实际的IO类型进行匹配()

[英]Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO ()

我有一个定义帐户的小程序,一个提现功能并尝试从中提现。 但是,它不会编译到期,并引发以下错误:

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功能。

注释在语义上确实没有任何作用,但是您可以考虑将其替换为空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM