2016-06-12 5 views
1

servantライブラリを使用してWeb APIへのクライアントバインディングを作成しようとしています。 JSONオブジェクトを送信できるようにします。私はそれをコンパイルしようとすると、SerJendクライアントからFromJSON contraintのタイプ変数

import   Control.Monad.Trans.Except (ExceptT, runExceptT) 
import   Data.Proxy 
import   Network.HTTP.Client (Manager) 
import   Servant.API 
import   Servant.Client 
import   Data.Aeson 

-- | This methods accepts any instance of 'ToJSON' 
-- I would like to have only this method exported from the module 
send :: ToJSON a => a -> Manager -> IO (Either ServantError Result) 
send x manager = runExceptT $ send_ x manager baseUrl 

type MyAPI a = "acceptAnyJson" 
    :> ReqBody '[JSON] a 
    :> Post '[JSON] Result 

api :: ToJSON a => Proxy (MyAPI a) 
api = Proxy 

send_ :: ToJSON a => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result 
send_ = client api 

は今、私はエラーメッセージを持っている:

Couldn't match type ‘a0’ with ‘a’ 
     because type variable ‘a’ would escape its scope 
    This (rigid, skolem) type variable is bound by 
     the inferred type for ‘send_’: 
     ... 

にはどうすれ型変数を受け入れるように私のMyAPIclientProxyをパラメータ化することができますか?

答えて

1

あなたが送っているものの種類にapiのタイプを結ぶ必要があります:

send_ = client (Proxy :: Proxy (MyAPI a)) 
:でも、その時点で apiを気

{-# LANGUAGE ScopedTypeVariables #-} 
send_ :: forall a. (FromJSON a) => a -> Manager -> BaseUrl -> ExceptT ServantError IO Result 
send_ = client (api :: Proxy (MyAPI a)) 

か、なぜ

関連する問題