1

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

我正在尝试运行的示例发布在此处: Yesod 博客示例 我对其进行了一些更改,我将类型添加到 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)
4

1 回答 1

5

自博客文章以来,类型发生generateFormPost了变化,它曾经是

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

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

于 2012-08-07T13:14:39.073 回答