0

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" 
Jodimoro
  • 4,355
  • 3
  • 11
  • 18

1 Answers1

3

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!

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • I don't want to use the extension, how to archive that without it? – Jodimoro May 09 '17 at 13:54
  • using "a" doesn't help. – Jodimoro May 09 '17 at 13:59
  • `ScopedTypeVariables` is totally harmless, universally supported, and a useful thing for everyday Haskell programming regardless. Here I don't even make use of that extension anyway, just use it to lift a somewhat arbitrary syntactic restriction. — But as I said: if you _use_ `a` (actually use it as a value, not just feed it to `show` – i.e., it must occur in a _monomorphic position_) then you don't need any explicit signature, either. Alternatively, you can use it in a polymorphic spot like `show` but give a signature there: `show (a :: Aeson.Object)`. That doesn't need `ScopedTypeVariables`. – leftaroundabout May 09 '17 at 14:08
  • I don't want to use the extension, how to archive that without it? – Jodimoro May 09 '17 at 14:10
  • @ThomasM.DuBuisson, ok. How can I extract a value from it? I've tried `.:` like this `myVal <- (a :: Object) .: "some_key" ` but it caused typed mismatch because it required the type `‘ActionCtxT ctx0 m0’` to return on each line inside the `do` – Jodimoro May 09 '17 at 15:06