在yesod博客示例中无法匹配预期类型

时间:2012-08-07 12:34:00

标签: haskell types yesod

我正在尝试运行博客示例,但某些处理程序函数中的类型存在问题,我不知道如何修复它。

我试图运行的示例发布在这里: Yesod blog example 我改变了一点,我将类型添加到defaultLayout函数并使其成为Yesod的实例以摆脱双重定义。

defLayout :: GWidget a Blog () -> GHandler a Blog RepHtml
defLayout inside = do
  mmsg <- getMessage
  pc <- widgetToPageContent $ do
      toWidget [lucius|
body {
width: 760px;
margin: 1em auto;
font-family: sans-serif;
}
textarea {
width: 400px;
height: 200px;
}
#message {
color: #900;
}
|]
      inside 
  hamletToRepHtml [hamlet|
$doctype 5
<html>
    <head>
        <title>#{pageTitle pc}
        ^{pageHead pc}
    <body>
        $maybe msg <- mmsg
            <div #message>#{msg}
        ^{pageBody pc}
|]

instance Yesod Blog where
    approot = ApprootStatic "http://localhost:3000"
    defaultLayout = defLayout

这些功能给我带来了问题:

getBlogR :: Handler RepHtml
getBlogR = do
  muser <- maybeAuth
  entries <- runDB $ selectList [] [Desc EntryPosted]
  ((_, entryWidget), enctype) <- generateFormPost entryForm
  defaultLayout $ do
             setTitleI MsgBlogArchiveTitle
             [whamlet|
$if null entries
    <p>_{MsgNoEntries}
$else
    <ul>
        $forall Entity entryId entry <- entries
            <li>
                <a href=@{EntryR entryId}>#{entryTitle entry}
$maybe Entity _ user <- muser
    $if isAdmin user
        <form method=post enctype=#{enctype}>
              ^{entryWidget}
              <div>
                  <input type=submit value=_{MsgNewEntry}>
$nothing
    <p>
         <a href=@{AuthR LoginR}>_{MsgLoginToPost}
|]

getEntryR :: EntryId -> Handler RepHtml
getEntryR entryId = do
  (entry, comments) <- runDB $ do
          entry <- get404 entryId
          comments <- selectList [] [Asc CommentPosted]
          return (entry, map entityVal comments)
  muser <- maybeAuth
  ((_, commentWidget), enctype) <- generateFormPost (commentForm entryId)
  defaultLayout $ do
    setTitleI $ MsgEntryTitle $ entryTitle entry
    [whamlet|
<h1>#{entryTitle entry}
<article>#{entryContent entry}
    <section .comments>
        <h1>_{MsgCommentsHeading}
        $if null comments
            <p>_{MsgNoComments}
        $else
             $forall Comment _entry posted _user name text <- comments
                 <div .comment>
                      <span .by>#{name}
                      <span .at>#{show posted}
                      <div .content>#{text}
        <section>
            <h1>_{MsgAddCommentHeading}
            $maybe _ <- muser
                <form method=post enctype=#{enctype}>
                    ^{commentWidget}
                    <div>
                        <input type=submit value=_{MsgAddCommentButton}>
            $nothing
                <p>
                    <a href=@{AuthR LoginR}>_{MsgLoginToComment}
|]

以下是我尝试运行时获得的输出:

blog.hs:147:4:
    Couldn't match expected type `GWidget Blog Blog ()'
                with actual type `(t0, t1)'
    In the pattern: (_, entryWidget)
    In the pattern: ((_, entryWidget), enctype)
    In a stmt of a 'do' block:
      ((_, entryWidget), enctype) <- generateFormPost entryForm

blog.hs:202:4:
    Couldn't match expected type `GWidget Blog Blog ()'
                with actual type `(t0, t1)'
    In the pattern: (_, commentWidget)
    In the pattern: ((_, commentWidget), enctype)
    In a stmt of a 'do' block:
      ((_, commentWidget), enctype) <- generateFormPost
                                         (commentForm entryId)

1 个答案:

答案 0 :(得分:5)

generateFormPost的类型自博客文章以来已发生变化,以前是

generateFormPost :: RenderMessage master FormMessage =>
                    (Html -> MForm sub master (FormResult a, xml)) ->
                    GHandler sub master ((FormResult a, xml), Enctype)
<{3>}中的

,但是yesod-form-0.4.*

generateFormPost :: RenderMessage master FormMessage => 
                    (Markup -> MForm sub master (FormResult a, xml)) -> 
                    GHandler sub master (xml, Enctype)

如果您使用yesod-form < 1,或者如果您更换

,那么[我认为,从未使用过yesod]的类型错误就会消失
((_, entryWidget), enctype) <- generateFormPost entryForm

(entryWidget, enctype) <- generateFormPost entryForm

反映generateFormPost

类型的变化