【问题标题】:Lift a value from a Monad - Yesod从 Monad 中提升一个值 - Yesod
【发布时间】:2014-05-12 21:59:46
【问题描述】:

我想在用户未登录时被重定向到登录页面时显示一条消息。

资源授权:

isAuthorized AdminR _ = isAdmin
isAuthorized _ _ = return Authorized

    isAdmin = do
            mu <- maybeAuthId
            return $ case mu of
                    Just "Foo" -> Authorized
                    Just _ -> Unauthorized "You are NOT a Admin !"
                    Nothing -> do
                                    setMessage "You have to Login "
                                    return AuthenticationRequired

错误:

Couldn't match type `m0 AuthResult' with `AuthResult'
Expected type: m0 () -> m0 AuthResult -> AuthResult
  Actual type: m0 () -> m0 AuthResult -> m0 AuthResult
In a stmt of a 'do' block: setMessage "You have to Login "
In the expression:
  do { setMessage "You have to Login ";
       return AuthenticationRequired }
In a case alternative:
    Nothing
      -> do { setMessage "You have to Login ";
              return AuthenticationRequired }

那么,如何从 Monad 中提取 AuthResult ?

【问题讨论】:

    标签: haskell yesod


    【解决方案1】:

    由于您在案例结束时运行 monad 操作,因此您无需返回案例。

    isAdmin = do
       mu <- maybeAuthId
       case mu of
            Just "Foo" -> Authorized
            Just _ -> Unauthorized "You are NOT a Admin !"
            Nothing -> do
                 setMessage "You have to Login "
                 return AuthenticationRequired
    

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 2017-01-04
      • 2023-03-29
      • 1970-01-01
      • 2021-09-21
      • 2017-08-15
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多