我从this和this之类的问题中了解到,使用PRAGMA OverloadedStrings
意味着我应该能够将Text用作字符串类型。
但是,当我用文本测试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您可能还想将默认(文本)添加到模块顶部 使其默认使用文本而不是字符串。
但是我不清楚允许该默认设置的语法是什么
答案 0 :(得分:5)
您已在文件中启用了ˋOverloadedStringsˋ,但这在ghci中也未启用。为此,您需要ˋ:set -XOverloadedStringsˋ。请注意,该扩展名会影响您编写字符串文字的位置,无论您在定义数据类型的位置是否启用该扩展名都无所谓。