Unable to fix "Ambiguous type variable" in Aeson and Spock

281 views Asked by At

I have a Spock application where I have this:

    post "/test" $ do
        a <- jsonBody'
        text "test"

It throws an exception:

• Ambiguous type variable ‘a0’ arising from a use of ‘jsonBody'’
      prevents the constraint ‘(Aeson.FromJSON a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Aeson.FromJSON Aeson.DotNetTime

thus I've tried solving it like this:

post "/test" $ do
        a <- jsonBody' :: Aeson.Object
        text "test" 

but have had no luck:

• Couldn't match type ‘ActionCtxT
                             () (WebStateM () MySession MyAppState) ()’
                     with ‘unordered-containers-0.2.8.0:Data.HashMap.Base.HashMap
                             T.Text b0’
      Expected type: hvect-0.4.0.0:Data.HVect.HVectElim
                       '[] (SpockActionCtx () () MySession MyAppState ())
        Actual type: unordered-containers-0.2.8.0:Data.HashMap.Base.HashMap
                       T.Text b0

How to fix it?

update:

this doesn't fix the problem:

        a <- jsonBody' :: Aeson.Object
        --a :: Aeson.Object <- jsonBody'
        let b = show a -- using a
        text "fdsfd" 
1

There are 1 answers

5
leftaroundabout On

a <- jsonBody' :: Aeson.Object gives Aeson.Object as the signature to jsonBody'. But that doesn't work: jsonBody' is not a value but an action from which such a value is obtained! You probably want to give that signature to a.

{-# LANGUAGE ScopedTypeSignatures #-}

post "/test" $ do
    a :: Aeson.Object <- jsonBody'
    text "test"

Really, you probably don't need anything like that though – just make sure you actually use a, then the compiler will probably be able to figure out its type on its own!