import Text.JSON
import Control.Monad
data Status = Status { user :: String, text :: String } deriving Show
makeStatus :: JSObject JSValue -> Result Status
makeStatus tweet = let (!) = flip valFromObj in do
userObject <- tweet ! "user"
user <- userObject ! "screen_name"
text <- tweet ! "text"
return Status {user = user, text = text}
[1 of 1] Compiling Main ( twitter.hs, interpreted )
twitter.hs:11:27:
Couldn't match expected type `[Char]'
with actual type `JSObject JSValue'
Expected type: String
Actual type: JSObject JSValue
In the `user' field of a record
In the first argument of `return', namely
`Status {user = user, text = text}'
Failed, modules loaded: none.
Remove let (!) = flip valFromObj in and replace tweet ! "user" with valFromObj "user" tweet etc., everything is fine.
This is caused by the monomorphism restriction, which causes the type of
(!)to be inferred asJSObject JSValue -> String -> Result (JSObject JSValue)because of its usage inAdding a type signature fixes the problem.