2016-06-14 22 views
2

一切是偉大的工作,直到大約一個月前...M.Map突然預計類型的錯誤

突然向我

berkson.github.io/source/blog.hs: 333, 42 
• Couldn't match type ‘unordered-containers-0.2.7.1:Data.HashMap.Base.HashMap 
          text-1.2.2.1:Data.Text.Internal.Text 
          aeson-0.11.2.0:Data.Aeson.Types.Internal.Value’ 
        with ‘M.Map [Char] [Char]’ 
    Expected type: M.Map [Char] [Char] 
    Actual type: Metadata 
• In the first argument of ‘(M.!)’, namely ‘md’ 
    In the first argument of ‘(++)’, namely ‘(md M.! "author")’ 
    In the second argument of ‘(++)’, namely ‘(md M.! "author") ++ "/"’ 

從代碼:

directorizeDateAndAuthor :: Routes 
directorizeDateAndAuthor = metadataRoute $ \md -> 
    gsubRoute "/[0-9]{4}-[0-9]{2}-[0-9]{2}-" $ \s -> 
     replaceAll "-" (const "/") s ++ (md M.! "author") ++ "/" 

我想知道你是否願意幫助我解釋它究竟告訴了我什麼?我知道在我的結尾有一些類型的語法錯誤,但我不明白是什麼改變了,爲什麼它不像以前那樣編譯?

價:https://github.com/berkson/berkson.github.io/blob/source/source/blog.hs#L330

+3

我的猜測是你升級了定義'Metadata'的包,並且該包從定義爲'Map'變爲'HashMap'。您需要更新此類用途才能使用'HashMap。!'而不是'M.!'。或者你可以改變你的cabal文件以依賴於舊版本的軟件包。 – luqui

+2

在hakyll 4.8'元數據'類型從'Map'改爲'Aeson.Object'(參見[發佈公告](https://groups.google.com/forum/#!topic/hakyll/M3SNUkH2zsQ)) –

+0

@JanTojnar GAH!謝謝你告訴我,但現在我更加迷失了!我認爲這只是一個替代本身的下降,而不是我得到之前的錯誤: '無法與實際類型'值'匹配預期類型'[Char]' 取而代之的是! > _ Berkson

回答

4

之前hakyll 4.8 Metadata類型同義詞被定義如下:

type Metadata = HashMap.Map String String 

元數據分別的鍵只是一個平坦的列表 - 值雙。

在hakyll 4.8中,元數據解析被改變(issue,commit,release anouncement)使用YAML解析器,以允許更復雜的元數據結構。現在,類型同義詞如下:

type Metadata = Aeson.Object 

這是Objectaeson包 - Data.Yaml庫共享的類型。

不幸的是,處理Aeson.Object並不像Map那樣簡單。要了解如何正確使用Aeson,您可以閱讀lengthy tutorial

幸運的是,碧玉爲我們提供了一個功能lookupString這幾乎是一個簡易替換的HashMap.!

(!) :: Metadata -> String -> String 
lookupString :: String -> Metadata -> Maybe String 

解纏也許值,然後你會得到類似下面的代碼:

directorizeDateAndAuthor :: Routes 
directorizeDateAndAuthor = metadataRoute $ \md -> 
    case lookupString "author" md of 
     Just author -> 
      gsubRoute "/[0-9]{4}-[0-9]{2}-[0-9]{2}-" $ \s -> 
       replaceAll "-" (const "/") s ++ author ++ "/" 
     Nothing -> error "The author metadata field is missing."