我有一个如下所示的函数:
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO ()
outputDelayCo Nothing = outputDelay Nothing
outputDelayCo (Just 1) = do exitSuccess
outputDelayCo (Just n) = outputDelay (Just (n-1))
我遇到了这个错误:
Couldn't match expected type ‘(Event -> ByteString)
-> [Event] -> Int -> IO ()’
with actual type ‘IO a0’
In a stmt of a 'do' block: exitSuccess
In the expression: do { exitSuccess }
我可以通过这种方式修复它,但这样做会更加丑陋:
outputDelayCo :: Maybe Int -> (Event -> ByteString) -> [Event] -> Int -> IO ()
outputDelayCo Nothing a b c = outputDelay Nothing a b c
outputDelayCo (Just 1) _ _ _ = do exitSuccess
outputDelayCo (Just n) a b c = outputDelay (Just (n-1)) a b c
我理解为什么会出现错误:因为exitSuccess将具有IO a返回类型,所以在该模式上类型不匹配。但是,正确/优雅的方法是什么?