我从这样的问题和这个问题中了解到,使用我应该能够使用 Text 作为我的字符串类型的方法。PRAGMA OverloadedStrings
但是,当我使用 Text 测试我的数据类型时,我收到以下错误:
$ 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
我究竟做错了什么?
我在这个问题中看到了以下建议:
编辑您可能还希望将默认(文本)添加到模块的顶部,以使其默认使用文本而不是字符串。
但我不清楚允许这个默认值的语法是什么