无法将类型“[Char]”与“Data.Text.Internal.Text”匹配
Could not match type ‘[Char]’ with ‘Data.Text.Internal.Text’
这似乎是一个热门问题。我根本不熟悉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
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$
起初,我接受了此处建议的可接受答案:。它有助于将错误数量减少到一个错误。现在编译脚本会导致这个错误,我不知道如何修复:
$ 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
| ^^^^^^^^^^^^
$
我应该如何更改我的代码来解决这个问题?
在Haskell中有多种常用字符串。默认情况下,字符串文字 ("stuff in quotes"
) 的类型为 String
(即 [Char]
),但您使用的库需要的值类型为 Text
.
启用 OverloadedStrings
扩展,这样字符串文字也可以具有类型 Text
将类型签名从 String
更改为 Text
; Text
可以从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>..."
这似乎是一个热门问题。我根本不熟悉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
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$
起初,我接受了此处建议的可接受答案:
$ 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
| ^^^^^^^^^^^^
$
我应该如何更改我的代码来解决这个问题?
在Haskell中有多种常用字符串。默认情况下,字符串文字 ("stuff in quotes"
) 的类型为 String
(即 [Char]
),但您使用的库需要的值类型为 Text
.
启用
OverloadedStrings
扩展,这样字符串文字也可以具有类型Text
将类型签名从
String
更改为Text
;Text
可以从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>..."