I'm a haskell newbie and currently trying to write my first productive haskell program. While I'm using the req package for restful client service like this:
test = req GET (http uriString) NoReqBody bsResponse (port 9090)
I got the following error message:
• No instance for (MonadHttp m0) arising from a use of ‘req’
However, if I add the type declaration, this error will be suppressed.
test :: (MonadHttp m) => m BsResponse
test = req GET (http uriString) NoReqBody bsResponse (port 9090)
My question is, since the definition of function req
is:
req ::
(MonadHttp m, HttpMethod method, HttpBody body,
HttpResponse response,
HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) =>
method
-> Url scheme
-> body
-> Data.Proxy.Proxy response
-> Option scheme
-> m response
The constraint of m
is already there, I think these is enough information for GHC to inference that the type of my test
function is :
test :: (MonadHttp m) => m BsResponse
Why I still need to add the type declaration manually/explicitly?
ps: I know that I should use the req library like this:
instance MonadHttp IO where
handleHttpException = throwIO
testGet :: IO BsResponse
testGet = do
let (url, options) = fromJust (parseUrlHttp (getList "2"))
req GET url NoReqBody bsResponse $ options <> port 9090
but the usage is still a little bit confusing, it seems like MonadHttp
is just an extra wrapper of MonadIO
and provide only exception handling which most of the time would be the same as all instances of MonadIO.
Is there any specific "design pattern" or "design philosophy" about API that suit for a new user of haskell ?