I’m trying to figure out Haskell’s json library. However, I’m running into a bit of an issue in ghci:
Prelude> import Text.JSON
Prelude Text.JSON> decode "[1,2,3]"
<interactive>:1:0:
Ambiguous type variable `a' in the constraint:
`JSON a' arising from a use of `decode' at <interactive>:1:0-15
Probable fix: add a type signature that fixes these type variable(s)
I think this has something to do with the a in the type signature:
decode :: JSON a => String -> Result a
Can someone show me:
- How to decode a string?
- What’s going with the type system here?
You need to specify which type you want to get back, like this:
If that line was part of a larger program where you would go on and use the result of
decodethe type could just be inferred, but since ghci doesn’t know which type you need, it can’t infer it.It’s the same reason why
read "[1,2,3]"doesn’t work without a type annotation or more context.