Haskell 使用 OverloadedStrings,但仍然出现 [Char] 错误
Haskell using OverloadedStrings, but getting [Char] error anyway
我从 and 这样的问题中了解到,使用 PRAGMA OverloadedStrings
意味着我应该能够使用文本作为我的字符串类型。
但是,当我用文本测试 my data types 时,出现以下错误:
$ stack ghci
Prelude> :l myfile.hs
Ok, one module loaded.
*Main> Rec "asd" "m"
<interactive>:46:5: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the first argument of ‘Rec’, namely ‘"asd"’
In the expression: Rec "asd" "m"
In an equation for ‘it’: it = Rec "asd" "m"
<interactive>:46:11: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the second argument of ‘Rec’, namely ‘"m"’
In the expression: Rec "asd" "m"
In an equation for ‘it’: it = Rec "asd" "m"
我的代码如下:
{-# LANGUAGE DeriveGeneric, OverloadedStrings, DefaultSignatures, TypeOperators, FlexibleContexts, RecordWildCards, FlexibleInstances, ExtendedDefaultRules #-}
import qualified Data.Map as Map
import qualified Data.Set as Set
-- import qualified Data.Text as T
import Data.Text (Text)
import GHC.Generics
data Rec = Rec {
recCategory :: Text,
recId :: Text
} deriving Generic
我做错了什么?
我在 this question 中看到一个建议:
EDIT You may also want to add default (Text) to the top of your module
to have it use Text instead of String by default.
但我不清楚允许此默认值的语法是什么
你在你的文件中启用了“OverloadedStrings”,但在 ghci 中并没有启用它。为此,你需要 ˋ:set -XOverloadedStringsˋ。请注意,扩展会影响您编写字符串文字的地方,无论您是否在定义数据类型的地方启用它都没有关系。
我从 PRAGMA OverloadedStrings
意味着我应该能够使用文本作为我的字符串类型。
但是,当我用文本测试 my data types 时,出现以下错误:
$ stack ghci
Prelude> :l myfile.hs
Ok, one module loaded.
*Main> Rec "asd" "m"
<interactive>:46:5: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the first argument of ‘Rec’, namely ‘"asd"’
In the expression: Rec "asd" "m"
In an equation for ‘it’: it = Rec "asd" "m"
<interactive>:46:11: error:
• Couldn't match expected type ‘Text’ with actual type ‘[Char]’
• In the second argument of ‘Rec’, namely ‘"m"’
In the expression: Rec "asd" "m"
In an equation for ‘it’: it = Rec "asd" "m"
我的代码如下:
{-# LANGUAGE DeriveGeneric, OverloadedStrings, DefaultSignatures, TypeOperators, FlexibleContexts, RecordWildCards, FlexibleInstances, ExtendedDefaultRules #-}
import qualified Data.Map as Map
import qualified Data.Set as Set
-- import qualified Data.Text as T
import Data.Text (Text)
import GHC.Generics
data Rec = Rec {
recCategory :: Text,
recId :: Text
} deriving Generic
我做错了什么?
我在 this question 中看到一个建议:
EDIT You may also want to add default (Text) to the top of your module to have it use Text instead of String by default.
但我不清楚允许此默认值的语法是什么
你在你的文件中启用了“OverloadedStrings”,但在 ghci 中并没有启用它。为此,你需要 ˋ:set -XOverloadedStringsˋ。请注意,扩展会影响您编写字符串文字的地方,无论您是否在定义数据类型的地方启用它都没有关系。