我有一个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"
a <- jsonBody' :: Aeson.Object
给出Aeson.Object
作为jsonBody'
的签名。但这行不通:jsonBody'
不是一个值,而是一个从中获得这种值的操作!您可能希望将该签名交给a
。
{-# LANGUAGE ScopedTypeSignatures #-}
post "/test" $ do
a :: Aeson.Object <- jsonBody'
text "test"
真的,你可能不需要这样的东西——只要确保你实际使用a
,那么编译器可能能够自己找出它的类型!