我正在嘗試構建this Hakyll blog。在嘗試編譯site.hs
,網站建設方案...構建網站時涉及HashMap和Map的類型錯誤
[email protected]:/usr/local# git clone https://github.com/relrod/blog elrod.me
[email protected]:/usr/local# cd elrod.me
[email protected]:/usr/local/elrod.me# ghc --make site.hs
...我獲得下列類型的錯誤:
[1 of 1] Compiling Main (site.hs, site.o) site.hs:28:70: error:
Couldn't match type ‘unordered-containers-0.2.7.2:Data.HashMap.Base.HashMap Data.Text.Internal.Text aeson-1.1.0.0:Data.Aeson.Types.Internal.Value’ with ‘M.Map [Char] a1’
Expected type: M.Map [Char] a1
Actual type: Metadata
In the third argument of ‘M.findWithDefault’, namely ‘metadata’
In the expression: M.findWithDefault "No title" "title" metadata
In an equation for ‘title’: title = M.findWithDefault "No title" "title" metadata
Relevant bindings include title :: a1 (bound at site.hs:28:25)
我怎樣才能解決這個問題?下面是site.hs
相關部分:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Map as M
import Data.Monoid ((<>))
import Hakyll
import Text.Pandoc.Options (readerSmart)
main :: IO()
main = hakyll $ do
-- etc.
match "posts/*" $ do
route $ setExtension "html"
compile $ do
let safetitle = field "safetitle" $ \item -> do
metadata <- getMetadata (itemIdentifier item)
let title = M.findWithDefault "No title" "title" metadata
return $ concatMap (\x -> if x == '\'' then "\\'" else [x]) title
pandocCompilerWith defaultHakyllReaderOptions {readerSmart = False} defaultHakyllWriterOptions
>>= saveSnapshot "content"
>>= loadAndApplyTemplate "templates/post.html" (postCtx tags <> safetitle)
>>= loadAndApplyTemplate "templates/default.html" defaultContext
>>= relativizeUrls
-- etc.
請編輯您的問題以添加足夠的解釋,以便我們不必猜測您正在嘗試做什麼(「製作'ghc --make site.hs'工作」只是不夠具體)。如果您不確定如何準備和組織您的問題,請考慮查看[tag:haskell]標記中最近出現的積極分數問題,以查看這裏的工作情況。 – duplode
問題是'getMetadata'返回一個'Metadata'類型,它是'aeson'' Object'的別名,它是'HashMap'的別名,但是有一個嘗試使用'Map'來訪問值' findWithDefault'而不是適當的'HashMap'查找函數('lookupDefault'?)。 – ryachza
@duplode我認爲原始問題包含顯示的確切錯誤消息,儘管格式不合適。我對格式化的東西進行了格式化,並將相關代碼片段包含在存儲庫中。 – ryachza