【发布时间】:2013-01-04 05:01:36
【问题描述】:
我正在尝试用一些额外的功能扩展Happstack crash course 博客:在主页上显示所有标签的列表。
我的博客记录是这样的:
data Blog = Blog
{ nextPostId :: PostId
, posts :: IxSet Post
, allTags :: [Text]
}
deriving (Data, Typeable)
我通过以下方式通过 id 获取博客文章(从速成课程中复制粘贴):
-- Models.hs
postById :: PostId -> Query Blog (Maybe Post)
postById pid =
do Blog{..} <- ask
return $ getOne $ posts @= pid
-- Controller.hs
viewPage :: AcidState Blog -> ServerPart Response
viewPage acid =
do pid <- PostId <$> lookRead "id"
mPost <- query' acid (PostById pid)
...
-- mPost has type Maybe Post here
...
而且它工作正常。
当我尝试以类似方式查询所有标签时:
-- Models.hs
getTags :: Query Blog [Text]
getTags =
do Blog{..} <- ask
return allTags
-- Controller.hs
serveTags :: AcidState Blog -> [Text]
serveTags acid = query' acid GetTags
这行不通。错误堆栈跟踪是:
Blog/Controllers.hs:154:18:
Couldn't match type `[Text]' with `Text'
Expected type: [Text]
Actual type: [acid-state-0.8.1:Data.Acid.Common.EventResult
GetTags]
In the return type of a call of query'
In the expression: query' acid GetTags
我不明白为什么query' 的返回类型是[EventResult GetTags],而它应该是[Text]。
这个错误的原因是什么?有办法解决吗?
【问题讨论】:
-
该错误消息向我表明
EventResult是一个类型函数,而EventResult GetTags是[Text],而您假设它应该是Text。 -
类型错误中提到包名的事实令人担忧。我怀疑以某种方式涉及多种版本的酸态。检查你的包数据库的完整性,包括关于你的编译过程的更多细节(你在使用 Cabal 吗?你提供了什么参数?)
-
只是想把它留在这里:感谢改进酸状态教程!
标签: haskell happstack acid-state