当我定义>>
函数(带RebindableSyntax
扩展名)并直接使用它(mempty >> 1 >> 2 >> 3
)时,一切都按预期工作,但是当我使用 do 块时,出现以下错误:
Couldn't match expected type ‘Integer’
with actual type ‘Aggregator Integer’
In a stmt of a 'do' block: 1
In the expression:
do mempty
1
2
3
In an equation for ‘notWorking’:
notWorking
= do mempty
1
2
3
Run Code Online (Sandbox Code Playgroud)
全码:
Couldn't match expected type ‘Integer’
with actual type ‘Aggregator Integer’
In a stmt of a 'do' block: 1
In the expression:
do mempty
1
2
3 …
Run Code Online (Sandbox Code Playgroud) 我需要一些包装在 IO 类型上,但我不能“强制”haskell 在包装器中运行 IO 操作。我尝试为它使用 bang 模式,但它也不起作用。
我在更简单的类型上重新创建了问题。下面的代码片段
{-# LANGUAGE BangPatterns #-}
newtype IOWrapper a = IOWrapper (IO a)
mkWrapper :: IO a -> IOWrapper a
mkWrapper !a = IOWrapper a
foo :: IO ()
foo = putStrLn "foo" --Shows "foo" on console, as expected
bar :: IOWrapper ()
bar = IOWrapper $ putStrLn "bar" --Shows nothing
baz :: IOWrapper ()
baz = mkWrapper $ putStrLn "baz" --Shows nothing
Run Code Online (Sandbox Code Playgroud) haskell ×2