我有以下代码
module MyLib (write) where
import Control.Monad.STM
import Control.Concurrent.STM.TVar
write :: STM (TVar Störm) -> Störm -> IO ()
write state' new = atomically $ write' state'
where
write' :: STM (TVar Störm) -> STM ()
write' state' = write'' <$> state'
write'' :: TVar Störm -> STM ()
write'' state' = writeTVar state' new
使用依赖关系STM
我收到以下错误
src/MyLib.hs:39:25: error:
• Couldn't match type ‘STM ()’ with ‘()’
Expected: TVar Störm -> ()
Actual: TVar Störm -> STM ()
• In the first argument of ‘(<$>)’, namely ‘write''’
In the expression: write'' <$> state'
In an equation for ‘write'’: write' state' = write'' <$> state'
|
39 | write' state' = write'' <$> state'
在我的测试中,它将Monad嵌套到STM (STM ())
。却不知道如何解决
1条答案
按热度按时间oxosxuxt1#
问题出在
write'' <$> state'
项上。在这种情况下,(<$>)
具有类型:和
但预期类型为
STM ()
。要解决这个问题,您可以执行以下操作:
但更简单的是:
或:
但是,我会这样写: