我是Haskell和Servant的新手,我正在努力获得正式的教程并运行。我一直试图让这个教程工作看看它并且无法让它整天工作。我讨厌在这里发布这样的问题,但老实说,我不知道为什么代码不是&#39因为我没有对它进行任何修改。我想知道是否有其他人试图实施本教程并遇到类似的问题。
我已经看过一些关于某些教程不再使用当前servant版本的帖子了,但本教程似乎是Servant Client最新的。
这是教程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
答案 0 :(得分:1)
这里的第一个“不在范围内”错误提到ClientM
类型,根据the servant-client changelog,它是 servant-client 的0.9版本引入的。在.cabal文件依赖项中指定servant-client >= 0.9
将确保cabal-install不会提取较旧的版本(尽管请注意,截至2020年3月,0.9本身已经很旧了)。对于cabal-install版本3,不需要进一步的配置步骤,因为cabal build
将自动安装任何指定的依赖项。