【问题标题】:Could not match type ‘[Char]’ with ‘Data.Text.Internal.Text’无法将类型“[Char]”与“Data.Text.Internal.Text”匹配
【发布时间】:2020-07-01 22:50:30
【问题描述】:

这似乎是一个热门问题。我根本不熟悉 Haskell,所以通过阅读类似问题的答案,我无法真正理解在我的情况下需要做什么。

我的脚本如下所示:

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

编译它会导致这些错误:

$ 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
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$

起初,我选择了此处建议的公认答案:Couldn't match expected type `Text' with actual type `[Char]'。它有助于将错误数量减少到一个。现在编译脚本会导致这个我不知道如何修复的错误:

$ 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
  |                                              ^^^^^^^^^^^^
$

我应该对我的代码进行哪些更改以解决此问题?

【问题讨论】:

  • 您使用的是哪个版本的编译器?使用 ghci v8.6.5,我将其作为 RawBlock 的类型签名:RawBlock :: Format -&gt; String -&gt; Block,并且您的代码开箱即用。在任何情况下,您都可以使用函数pack 将字符串转换为文本。
  • @jpmarinier pandoc 终于在 2.8 版中从 String 切换到 Text。您可能安装了旧版本。
  • 附加评论:除了使用 Haskell 过滤器,还可以调整 this Lua 过滤器。这将避免一些数据序列化,并且可能会更快。
  • @tarleb 正确,运行 Linux Fedora v31 提供的 Pandoc 2.5。显然,它们并没有超越向后兼容性。
  • @gmile 没错,它主要针对 Markdown 输入(可能包括原始 LaTeX)。我很高兴从您对 pandoc 问题的帖子中看到您找到了一种使其适应您的要求的方法。

标签: haskell pandoc


【解决方案1】:

在 Haskell 中有多种常用的字符串类型。默认情况下,字符串文字 ("stuff in quotes") 的类型为 String(即 [Char]),但您使用的库需要 Text 类型的值。

  • 启用OverloadedStrings 扩展,以便字符串文字也可以具有Text 类型

  • 将类型签名从String更改为TextText 可以从 text 库中的Data.Text 模块导入(还值得一提的是,有两个Text,另一个来自Data.Text.Lazy,这可能是另一个来源将来与您不匹配)。

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

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多