oldhomemovie
oldhomemovie

Reputation: 15129

Could not match type ‘[Char]’ with ‘Data.Text.Internal.Text’

This seems like a popular question. I am not familiar with Haskell at all, so by reading the answers to the similar questions I can't really understand what needs to be done in my case.

My script looks like this:

import Text.Pandoc.JSON

pagebreakXml :: String
pagebreakXml = "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>"

pagebreakBlock :: Block
pagebreakBlock = RawBlock (Format "openxml") pagebreakXml

blockSwapper :: Block -> Block
blockSwapper (Para [Str "<div class=\"docxPageBreak\"></div>"])  = pagebreakBlock
blockSwapper blk = blk

main = toJSONFilter blockSwapper

Compiling it results in these errors:

$ ghc --make docx-page-filter.hs -package-db=/Users/eugene/.cabal/store/ghc-8.8.3/package.db
[1 of 1] Compiling Main             ( docx-page-filter.hs, docx-page-filter.o )

docx-page-filter.hs:7:35: error:
    • Couldn't match expected type ‘Data.Text.Internal.Text’
                  with actual type ‘[Char]’
    • In the first argument of ‘Format’, namely ‘"openxml"’
      In the first argument of ‘RawBlock’, namely ‘(Format "openxml")’
      In the expression: RawBlock (Format "openxml") pagebreakXml
  |
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
  |                                   ^^^^^^^^^

docx-page-filter.hs:7:46: error:
    • Couldn't match type ‘[Char]’ with ‘Data.Text.Internal.Text’
      Expected type: Data.Text.Internal.Text
        Actual type: String
    • In the second argument of ‘RawBlock’, namely ‘pagebreakXml’
      In the expression: RawBlock (Format "openxml") pagebreakXml
      In an equation for ‘pagebreakBlock’:
          pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
  |
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
  |                                              ^^^^^^^^^^^^

docx-page-filter.hs:10:25: error:
    • Couldn't match expected type ‘Data.Text.Internal.Text’
                  with actual type ‘[Char]’
    • In the pattern: "<div class=\"docxPageBreak\"></div>"
      In the pattern: Str "<div class=\"docxPageBreak\"></div>"
      In the pattern: [Str "<div class=\"docxPageBreak\"></div>"]
   |
10 | blockSwapper (Para [Str "<div class=\"docxPageBreak\"></div>"])  = pagebreakBlock
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$

At first, I went with an accepted answer suggested here: Couldn't match expected type `Text' with actual type `[Char]'. It helped reduce the number of errors to a single one. Compiling the script now results in this error that I don't know how to fix:

$ ghc --make docx-page-filter.hs -package-db=/Users/eugene/.cabal/store/ghc-8.8.3/package.db -XOverloadedStrings
[1 of 1] Compiling Main             ( docx-page-filter.hs, docx-page-filter.o )

docx-page-filter.hs:7:46: error:
    • Couldn't match type ‘[Char]’ with ‘Data.Text.Internal.Text’
      Expected type: Data.Text.Internal.Text
        Actual type: String
    • In the second argument of ‘RawBlock’, namely ‘pagebreakXml’
      In the expression: RawBlock (Format "openxml") pagebreakXml
      In an equation for ‘pagebreakBlock’:
          pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
  |
7 | pagebreakBlock = RawBlock (Format "openxml") pagebreakXml
  |                                              ^^^^^^^^^^^^
$

What should I change about my code to fix this issue?

Upvotes: 3

Views: 798

Answers (1)

Li-yao Xia
Li-yao Xia

Reputation: 33569

In Haskell there are multiple types of strings in common usage. By default, string literals ("stuff in quotes") are of type String (which is [Char]), but the library you're using expects values of type Text.

  • Enable the OverloadedStrings extension so that string literals can also have type Text

  • Change the type signature(s) from String to Text; Text can be imported from the Data.Text module in the text library (it's also worth mentioning that there are two Text, the other one from Data.Text.Lazy, and that could be another source of mismatch for you in the future).

{-# LANGUAGE OverloadedStrings #-}   -- Add at the top of the file

...  -- imports
import Data.Text (Text)              -- Import the Text type

pagebreakXml :: Text                 -- from String to Text
pagebreakXml = "<w:p>..."

Upvotes: 4

Related Questions