Haskell xml-conduit without OverloadedStrings?

130 views Asked by At

Every example I have seen online of Haskell's xml-conduit module uses the OverloadedStrings GHC extension (e.g. here). I assume this is because the Text.XML.Cursor.element function has type Name -> Axis. For example, this snippet would not work without OverloadedStrings:

{-# LANGUAGE OverloadedStrings #-}
import Text.XML
import Text.XML.Cursor
import Data.Text (Text)
import Data.Text.Read (decimal)
import Data.Monoid (mconcat)

main :: IO ()
main = do
    doc <- Text.XML.readFile def "people2.xml"
    let cursor = fromDocument doc
    print $ cursor $// element "{http://example.com}person" >=> parsePerson

data Person = Person Int Text
        deriving Show

parsePerson :: Cursor -> [Person]
parsePerson c = do
    let name = c $/ element "{http://example.com}firstname" &/ content
        ageText = c $/ element "{http://example.com}age" &/ content
    case decimal $ mconcat ageText of
        Right (age, "") -> [Person age $ mconcat name]
        _ -> []

However, I want to write a module that is more portable, i.e. without using this extension. What is the best way of doing this? Am I doomed to create a Text value from each string with Data.Text.pack and then using the Text.XML.Name data constructor before passing the result to element? Or is there some easier way?

1

There are 1 answers

0
Levi Pearson On BEST ANSWER

I'm afraid the "easier way" is -XOverloadedStrings. I'm also afraid you're not going to be able to meet your portability goals while relying on xml-conduit, because it makes liberal use of language extensions itself.

Unless you have a specific alternative compiler you are targeting, you might as well use a -XOverloadedStrings, especially since xml-conduit was explicitly designed to support it.