我们如何让用户传递一个 eventHandler,它使用 stateMonad 但在单独的线程中调用?比如下面的例子中,应该如何调用forkIO,以便eventHandler可以调用操作呢?我是 Haskell 的新手,如果这是向用户公开的错误 api,请纠正我?
data MyTypeResult a = MyTypeValue a
data MyTypeState = MyTypeState {_counter :: Int}
newtype MyType a = MyType {
unMyType :: StateT MyTypeState IO (MyTypeResult a)
}
instance Monad MyType where
(>>=) = myTypeBind
return = myTypeReturn
fail = myTypeFail
myTypeBind = undefined
myTypeReturn = undefined
myTypeFail = undefined
type Event = String
type Handler = Event -> MyType ()
doSomethingAwesome :: MyType Event
doSomethingAwesome = undefined
operate :: String -> MyType ()
operate = undefined
start :: Handler -> MyType ()
start h = do
event <- doSomethingAwesome
--forkIO $ h event -- The line that is troubling
return ()
testHandler :: Event -> MyType()
testHandler _ = operate "abcd"
myMain = start testHandler