using Text instead of String - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Vincent L

Hi, I'd like to use Textinstead of String, afaiu it's more performant. How can I create a "text literal" ? "\r\n" for instance is interpreted as a String, is there a special syntax or should I use pack ?

TheMatten

Enable OverloadedStrings

Vincent L

and is there a "read" equivalent for Text ?

TheMatten

(BTW, this means that string literals will have type IsString a => a, so if you get some "ambiguous" errors, just stick type annotation next to it)

None that I know of - I tend to do something like slapping

read :: Read a => Text -> a
read = Prelude.read . T.unpack

into custom Prelude module

Sridhar Ratnakumar

If writing production code, avoid read (as it is a partial function), and prefer readMaybe.

Mats Rauhala

Or readEither (though the Left side of it is quite useless)