Gabriella439 / Gabriella439/Haskell-Errors-Library
Add function to convert MonadPlus (as MaybeT) to MonadError
- Dominant language
- Haskell
- Stars
- 65
- Forks
- 22
- PR merge metrics
- No merged PRs in 30d
Description
I apologize if there's already a straight forward way to do this, and I just couldn't figure it out. In the popular setup where you have a `ReaderT` instance for your application monad, you might end up writing functions that return `m (Maybe String)`. For example, if you're fetching something from the database, then that entity may or may not be there.
To make it easier to compose different functions that all share the application monad, but not the return type, you might decide to change this to `(MonadPlus m) => m String`. The most convenient way for me to then handle the `mzero` case is through the `MaybeT` instance of `MonadPlus`. And so I often have this in my code:
```haskell
(?*) :: (MonadError e m) => MaybeT m b -> e -> m b
(?*) x e = runMaybeT x >>= liftEither . note e
foobar :: (HasDB m, MonadError MyError m, MonadThrow m) => Vault.Key -> Wai.Request -> UserID -> m Wai.Vault
foobar vaultKey request userId = do
-- Role.get has `MonadPlus m`
roles <- Role.get userId ?* NoRoles -- NoRoles is a constructor for MyError
let vault' = Vault.insert vaultKey (roles, userId) $ Wai.vault request
return vault'
```
which is a variation of the `??` infix operator already in the library.
Is this something that might be added to this library or is there a better way of achieving the above with what's already there?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.