Skip to content

Instantly share code, notes, and snippets.

@LeifW
Created July 12, 2018 18:14
Show Gist options
  • Save LeifW/f3e51437f4494eff9d7c23355f268ef7 to your computer and use it in GitHub Desktop.
Save LeifW/f3e51437f4494eff9d7c23355f268ef7 to your computer and use it in GitHub Desktop.
Aeson ambiguous instance
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
thing = object [ "foo" .= "bar" ]
@LeifW
Copy link
Author

LeifW commented Jul 12, 2018

Gives:

test.hs:5:18: error:
    • Ambiguous type variable ‘v0’ arising from a use of ‘.=’
      prevents the constraint ‘(ToJSON v0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘v0’ should be.
      These potential instances exist:
        instance ToJSON DotNetTime
          -- Defined in ‘aeson-1.3.1.1:Data.Aeson.Types.ToJSON’
        instance ToJSON Value
          -- Defined in ‘aeson-1.3.1.1:Data.Aeson.Types.ToJSON’
        instance (ToJSON a, ToJSON b) => ToJSON (Either a b)
          -- Defined in ‘aeson-1.3.1.1:Data.Aeson.Types.ToJSON’
        ...plus 25 others
        ...plus 55 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: "foo" .= "bar"
      In the first argument of ‘object’, namely ‘["foo" .= "bar"]’
      In the expression: object ["foo" .= "bar"]
  |
5 | thing = object [ "foo" .= "bar" ]
  |                  ^^^^^^^^^^^^^^

test.hs:5:27: error:
    • Ambiguous type variable ‘v0’ arising from the literal ‘"bar"’
      prevents the constraint ‘(Data.String.IsString
                                  v0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘v0’ should be.
      These potential instances exist:
        instance Data.String.IsString Value
          -- Defined in ‘aeson-1.3.1.1:Data.Aeson.Types.Internal’
        instance (a ~ Char) => Data.String.IsString [a]
          -- Defined in ‘Data.String’
        ...plus 11 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the second argument of ‘(.=)’, namely ‘"bar"’
      In the expression: "foo" .= "bar"
      In the first argument of ‘object’, namely ‘["foo" .= "bar"]’
  |
5 | thing = object [ "foo" .= "bar" ]

And yet it works fine in the repl:

ghci -XOverloadedStrings
GHCi, version 8.4.3: http://www.haskell.org/ghc/  :? for help
Prelude> import Data.Aeson
Prelude Data.Aeson> object [ "foo" .= "bar" ]
Object (fromList [("foo",String "bar")])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment