1

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

Mittenchops
  • 18,633
  • 33
  • 128
  • 246

1 Answers1

12

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.

Cubic
  • 14,902
  • 5
  • 47
  • 92
  • Huh. Is there a syntax that would ask my ghci session to match the pragma requested on the module I'm loading? I'm trying to load a lot of pragma as I work on this file--it seems like it would be easy to miss something in `:set -XOverloadedStrings -XDeriveGeneric" etc., and kind of cumbersome to type up given it's already in the file. – Mittenchops Apr 11 '19 at 20:51
  • @Mittenchops not as far as I’m aware, and I don’t think it’d be as useful as you’re imagining, because many language extensions aren’t needed at use side (and in this case it’s unneeded in your definition). You can set s number of default extensions to turn on though, depending on how exactly you start ghci. – Cubic Apr 11 '19 at 20:56
  • 3
    Add your favorites to `~/.ghci` – luqui Apr 12 '19 at 06:05