我是 Haskell 和 Servant 的新手,正在尝试启动并运行官方教程。我一直在努力让本教程工作以查看它,但一整天都无法让它工作。我讨厌在这里发布这样的问题,但老实说我不知道为什么代码不能正常工作我没有对其进行任何更改。我想知道是否有其他人尝试实施本教程并遇到类似的问题。
我已经看到一些关于不再使用当前仆人版本的教程的帖子,但本教程似乎是仆人客户端的最新教程。
这是教程http://haskell-servant.readthedocs.io/en/stable/tutorial/Client.html
这是代码
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
module Client where
import Data.Aeson
import Data.Proxy
import GHC.Generics
import Network.HTTP.Client (newManager, defaultManagerSettings)
import Servant.API
import Servant.Client
data Position = Position
{ x :: Int
, y :: Int
} deriving (Show, Generic)
instance FromJSON Position
newtype HelloMessage = HelloMessage { msg :: String }
deriving (Show, Generic)
instance FromJSON HelloMessage
data ClientInfo = ClientInfo
{ clientName :: String
, clientEmail :: String
, clientAge :: Int
, clientInterestedIn :: [String]
} deriving Generic
instance ToJSON ClientInfo
data Email = Email
{ from :: String
, to :: String
, subject :: String
, body :: String
} deriving (Show, Generic)
instance FromJSON Email
type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
:<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
:<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
position :: Int -> Int -> ClientM Position
hello :: Maybe String -> ClientM HelloMessage
marketing :: ClientInfo -> ClientM Email
api :: Proxy API
api = Proxy
position :<|> hello :<|> marketing = client api
queries :: ClientM (Position, HelloMessage, Email)
queries = do
pos <- position 10 10
message <- hello (Just "servant")
em <- marketing (ClientInfo "Alp" "alp@foo.com" 26 ["haskell", "mathematics"])
return (pos, message, em)
run :: IO ()
run = do
manager <- newManager defaultManagerSettings
res <- runClientM queries (ClientEnv manager (BaseUrl Http "localhost" 8081 ""))
case res of
Left err -> putStrLn $ "Error: " ++ show err
Right (pos, message, em) -> do
print pos
print message
print em
这是我得到的错误
Couldn't match type ‘http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position’ with ‘Control.Monad.Trans.Except.ExceptT ServantError IO Position’ Expected type: Int -> Int -> ClientM Position Actual type: Int -> Int -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position • When checking that the inferred type position :: Int -> Int -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position is as general as its signature position :: Int -> Int -> ClientM Position Variable not in scope: runClientM :: ClientM (Position, HelloMessage, Email) -> t0 -> IO (Either a0 (a1, a2, a3)) Data constructor not in scope: ClientEnv :: http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> t0