我想定义一个记录器函数,比如
myPutStrLn = putStrLn . (++) "log: "
main = do myPutStrLn "hello"
这很好。现在我想用 格式化提供的字符串printf
,像这样
myPutStrLn $ printf "test %d" (23 :: Int)
伟大的!由于我经常使用这种模式,因此我想将其printf
纳入记录器功能:
myPrintf = logger . printf
where
-- note, this is just an example. should be
-- replaceable with any function with this
-- typesignature
logger :: String -> IO ()
logger = putStrLn . (++) "log: "
main = myPrintf "test %d" (23 :: Int)
不幸的是,这失败了
The function `myPrintf' is applied to two arguments,
but its type `String -> IO ()' has only one
In a stmt of a 'do' block: myPrintf "test %d" (23 :: Int)
In the expression: do { myPrintf "test %d" (23 :: Int) }
In an equation for `main':
main = do { myPrintf "test %d" (23 :: Int) }
GHC 推断myPrintf :: String -> IO ()
,所以显然有问题。我发现了有关Polyvariadic composition的一些信息,但我无法将其应用于我的问题。我什至不确定它是否能解决我的问题。
该代码也可通过gist获得。