1

If I want to use readFile from Data.Text.IO, but there's already a readFile in the Prelude, how do I import it, so that it doesn't cause the ambiguity error?

I have a script that just says import Data.Text.IO and then later uses readFile, and I'm testing it in ghci using :load, but it complains about ambiguous function calls.

Jonathan
  • 10,571
  • 13
  • 67
  • 103
  • 1
    `import qualified Data.Text.IO as T` and `T.readFile` – user2407038 Jan 11 '18 at 00:10
  • See also [my writing on the Haskell module system and how it interacts with name collision issues](https://stackoverflow.com/a/8331995/791604) on another question. You can stop reading where it says "Here ends the bare basics you need to understand about the module system.". The answers below are imprecise about which bits of their suggestions are *required* to fix the problem and which are just standard practice, which hopefully my other answer will clarify if you care, and will suggest several other solutions (e.g. keep the same import, but use `Data.Text.IO.readFile` instead of `readFile`). – Daniel Wagner Jan 11 '18 at 01:49

2 Answers2

7

There are two solutions.

import qualified Data.Text.IO as T

This will give you the Data.Text.IO function as T.readFile. However, if you only plan to use the Data.Text.IO version and never the Prelude version, you can exclude the Prelude version.

import Prelude hiding (readFile)
import Data.Text.IO

An explicit import Prelude will override the default import of Prelude, and you can control which names get imported.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
2

import qualified Data.Text.IO as DTIO, then you can use DTIO.readfile.

see https://wiki.haskell.org/Import for details

delta
  • 3,778
  • 15
  • 22