0

I understand from the sample notebook that I should be able to enable and disable extensions as follows:

-- We can disable extensions. 
:ext NoEmptyDataDecls 
data Thing

<interactive>:1:1: error:
    • ‘Thing’ has no constructors (EmptyDataDecls permits this)
    • In the data declaration for ‘Thing’

-- And enable extensions.
:ext EmptyDataDecls
data Thing

However, when I try this with OverloadedStrings, I do not see any success. You can see from the below that T.lines is looking for String rather than Text. Why?

enter image description here

What am I misunderstanding or doing wrong?

Mittenchops
  • 18,633
  • 33
  • 128
  • 246
  • 3
    [readFile](https://hoogle.haskell.org/?hoogle=readFile) has type `FilePath -> IO String`. Using `OverloadedStrings` does not magically mean all `String` values turn into `Text` (or `ByteString` etc.), it just makes string *literals* polymorphic. It's just like with numbers, the literal `2`, say, can be any instance of `Num`, but this doesn't mean that you can use a function that returns an `Int` and use its result as if it were a `Double`. – Robin Zigmond Apr 11 '19 at 22:11

1 Answers1

0

Good news: the notebook above does have OverloadedStrings loaded correctly. The problem is you need to read the file with:

T.readFile

So

main :: IO ()
main = do
  text <- T.readFile "./data.txt"
  print $ T.lines text

This was confusing because the error highlighted T.lines rather than readFile. It turns out readFile does not produce a form of textual data that will automatically cast to the format required by T.lines (it produces String, not Text). You had to know that there is an entirely other function to call that does do that. The type system will not convert between these string-like formats for you. You must do this yourself by calling a file-reading function that explicitly returns a Text: here, T.readFile.

Mittenchops
  • 18,633
  • 33
  • 128
  • 246