【问题标题】:Add print in Scotty do block在 Scotty do block 中添加打印
【发布时间】:2018-08-15 19:47:50
【问题描述】:

我对 Haskell 很陌生,如果问题很愚蠢,请提前抱歉,但我无法在 google 中找到解决方案。

假设我有这个程序使用Scotty web 框架:

responseUserByName :: ActionM ()
responseUserByName = do name <- param "name"
                        user <- liftAndCatchIO $ getUserByUserName name
                        json user

我想添加一个日志,因为它在运行时失败,我不知道为什么。所以我的想法是在 do 块中添加一些打印来检查值。

但由于do 块必须返回ActionM,我无法打印并返回IO。好吧,或者至少我不知道怎么做。

问候

【问题讨论】:

    标签: haskell scotty


    【解决方案1】:

    我猜ActionM 来自Scotty。在这种情况下,您可以简单地使用 liftIO 解除 IO 操作,就像您已经使用 liftAndCatchIO 所做的那样:

    responseUserByName :: ActionM ()
    responseUserByName =
        do name <- param "name"
           user <- liftAndCatchIO $ getUserByUserName name
           liftIO $ putStrLn "this is a log message"
           json user
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2023-03-18
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      相关资源
      最近更新 更多