0

我有一个 Spock 应用程序,其中有:

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

它抛出一个异常:

• 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

因此我尝试像这样解决它:

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

但没有运气:

• 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

如何解决?

更新:

这不能解决问题:

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

1 回答 1

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!

于 2017-05-09T13:22:11.343 回答