Axe*_*kel 1 haskell types yesod
我正在尝试将网页的标题设置为包含当前年份的字符串,如下所示:
getCurrentYear :: IO String
getCurrentYear = do
now <- getCurrentTime
let today = utctDay now
let (year, _, _) = toGregorian today
return $ show year
title :: IO Html
title = do
y <- getCurrentYear
return $ toHtml $ "Registration " ++ y
getRootR :: Handler RepHtml
getRootR = do
(widget, enctype) <- generateFormPost personForm -- not important for the problem at hand, comes from the example in the yesod book
defaultLayout $ do
setTitle title -- this is where I get the type error
[...]
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我setTitle在行中收到以下错误:
Couldn't match expected type `Html' with actual type `IO Html'
In the first argument of `setTitle', namely `title'
[...]
Run Code Online (Sandbox Code Playgroud)
我只是无法将当前年份从IO monad中取出(或将setTitle功能提升到其中).我尝试了各种各样的事情没有成功,所以这可能归结为我仍然没有理解Haskell的类型系统;-)你能启发我吗?
你title的Html价值不是本身.它被包裹在里面IO.你必须先提取它.
getRootR = do
[...]
defaultLayout $ do
unwrappedTitle <- title
setTitle unwrappedTitle
[...]
Run Code Online (Sandbox Code Playgroud)