4

I run often in the problem that GHC interprets "some text" as String when I need Text in places where both are acceptable, only to find later the error in another use and forcing an explicit type annotation.

Is there a way to set a preference for GHC to prefer Text and not String? The question is likely the same as this one but the only answer there is very heavy and not likely usable for me. Is there a better one?

I would like to be able to set a preference, that unqualified "some text" is of type Text to avoid the error in the following contrived example:

import Data.Text

some = "some"
text1 = "text1":: Text 

two = Data.Text.concat [some, text1]
user855443
  • 2,596
  • 3
  • 25
  • 37
  • I don't think there's a way around `RebindableSyntax`. You need `OverloadedStrings` to make string literals able to be used as anything other than `String` **at all**, but then you need `RebindableSyntax` to make them desugar to something other than calls to the builtin `fromString` (and the builtin `fromString` supports returning `String`). If that's the answer this is a duplicate question, really; hopefully someone else has something clever though. – Ben Aug 13 '23 at 14:14
  • The issue you're describing isn't in the example you gave. You just forgot to enable `OverloadedStrings`. I've answered based on the issue you described in the first paragraph. – 4castle Aug 14 '23 at 05:31

2 Answers2

4

You can put a default declaration at the top of the module so that Text will be defaulted for any ambiguous IsString values.

default (Integer, Double, Text)

In your code, the type is not ambiguous because you specified a Text-only function. The error just results from not enabling OverloadedStrings. Here is a truly ambiguous example that would benefit from a default declaration.

{-# LANGUAGE OverloadedStrings #-}

import Data.Text

default (Integer, Double, Text)

some = "some"

main = print some

Inspecting this in GHCi shows that :t some is Text. Without the default declaration, :t some is [Char] (i.e. String).

4castle
  • 32,613
  • 11
  • 69
  • 106
  • This is exactly what I was looking for and did not find in the docs... (I have `OverloadedStrings` enabled and forgot to show it in the question; thank you for spotting it!) – user855443 Aug 16 '23 at 21:17
0

Just enable the OverloadedStrings extension, this can be done by adding {-# LANGUAGE OverloadedStrings #-} to the top of the file.

This will automatically choose the appropriate 'string' type.

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • 3
    Maybe we need some clarification, but I think the OP was already using `OverloadedStrings`. Without that extension, there is never any ambiguity; GHC will simply always interpret `"quoted-strings"` as expressions of type `String`. There's no choice that one could have a preference about which way to resolve, and adding an explicit type annotation will not change anything (you need to actually call a conversion function like `pack`). Also the OP links to another question where the starting point is `OverloadedStrings` already being in use. – Ben Aug 13 '23 at 14:09