haskellgenericsoverloaded-strings

Haskell using OverloadedStrings, but getting [Char] error anyway


I understand from questions like this and this that using the PRAGMA OverloadedStrings means I should be able to use Text as my string-type.

However, as I test out my data types with Text, I get the following error:

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

My code is as follows:

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

What am I doing wrong?

I see in this question a suggestion to:

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.

But it's not clear to me what the syntax would be that allows this default


Solution

  • You have enabled ˋOverloadedStringsˋ in your file, but that doesn’t enable it in ghci as well. You’d need ˋ:set -XOverloadedStringsˋ for that. Note that the extension affects places where you write string literals, it doesn’t matter whether or not it was enabled where you defined your data type.