如何在 "ExceptT e m a "do块中运行 "m (Either e a) "动作?

问题描述 投票:2回答:1

第一次尝试使用变换器(算是吧),我想使用下面的函数。

getEnvList :: Text -> IO (Either String [Text])

在do块中

type EitherIO a = ExceptT String IO a
script :: EitherIO ()

我想我应该可以做到这一点。

entryKeys :: [Text] <- pure $ getEnvList active_cac_entries

但是,我得到了这个错误。

    • Couldn't match expected type ‘IO (Either String [Text])’
                  with actual type ‘[Text]’
    • When checking that the pattern signature: [Text]
        fits the type of its context: IO (Either String [Text])
      In the pattern: entryKeys :: [Text]
      In a stmt of a 'do' block:
        entryKeys :: [Text] <- pure $ getEnvList active_cac_entries
   |
93 |   entryKeys :: [Text] <- pure $ getEnvList active_cac_entries
   |   ^^^^^^^^^^^^^^^^^^^
haskell monad-transformers
1个回答
4
投票

在这种情况下,正确的函数是 ExceptT 构造者。

ExceptT :: m (Either e a) -> ExceptT e m a

-- do ...
--    entry <- ExceptT $ getEnvList active_cac_entries
--    ...
© www.soinside.com 2019 - 2024. All rights reserved.