Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

haskell - M.Map sudden expected type error

Everything was working great up until about a month or so ago...

Suddenly I'm getting

 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") ++ "/"’

From the code:

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

I was wondering if you'd mind helping me decipher what exactly it's telling me? I get that there's some type of syntax error on my end, but I don't understand what changed and why it's not compiling like it used to?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Before hakyll 4.8 the Metadata type synonym was defined as follows:

type Metadata = HashMap.Map String String

The metadata were just a flat list of key – value pairs.

In hakyll 4.8, metadata parsing was changed (issue, commit, release anouncement) to use YAML parser, to allow more complex metadata structure. Now, the type synonym is following:

type Metadata = Aeson.Object

This is the Object from aeson package – Data.Yaml library shares the types.

Unfortunately, handling the Aeson.Object is not as straightforward as the Map was. To learn how to use Aeson properly, you can read a lengthy tutorial.

Luckily, Jasper provided us with a function lookupString which is almost a drop-in replacement of HashMap.!:

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

Unwrapping the Maybe value, you will then get something like the following code:

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."

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...