Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a MonadThrow typeclass. #33

Open
tmciver opened this issue Nov 25, 2018 · 0 comments
Open

Create a MonadThrow typeclass. #33

tmciver opened this issue Nov 25, 2018 · 0 comments

Comments

@tmciver
Copy link
Owner

tmciver commented Nov 25, 2018

The MonadThrow class might look something like this:

interface MonadThrow extends Monad {
  function throwM($errObj);
}

This will make it easier to make methods generic in their return monads. For example, one could write a database-querying method like so:

class MyRepository {
  function getEntityById(string $id, $monadThrow = new MaybeMonad()) {
    $dbEntity = ... // query the database

    return is_null($dbEntity) ?
      $monadThrow->throwM("Entity with ID: " . $id . " could not be found.") :
      $monadThrow->pure($dbEntity);
  }
}

If the caller desires the return type to be Maybe, they could simply call it like this:

$maybeEntity = $repository->getEntityById("123456");

But if, say, an Attempt is desired, then they could call it like this:

$attemptEntity = $repository->getEntityById("123456", new AttemptMonad());

This presumes, of course, that both MaybeMonad and AttemptMonad have implemented the MonadThrow interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant