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

Generalize types of 'whenJust' and similar functions to return 'Monoid' #150

Open
chshersh opened this issue Feb 27, 2018 · 3 comments
Open
Labels
feature request have to think type:breaking Breaking change (removal, renaming, semantic change, etc.)

Comments

@chshersh
Copy link
Contributor

Currently whenJust has the following type signature:

whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()

I propose to generalize types of whenJust and whenJustM functions to the following:

whenJust :: forall m f . (Applicative f, Monoid m) => Maybe a -> (a -> f m) -> f m

or even following:

whenJust :: Monoid m => Maybe a -> (a -> m) -> m

() has trivial Monoid instance so every existing code will work automatically. Also, IO has Monoid instance.

Drawbacks:

  1. More obscure type signature (probably?).
  2. Type errors can be hard to understand.

Advantages

  1. There's no single function to simulate foldMap :: (Monoid m, Foldable t) => (a -> m) -> t a -> m behavior for Maybe.
  2. This type signature will allow to make code like foo <- if p then listDirectory else pure [] shorter. Sometimes on Nothing case we want not only pure () but return some mempty
@gromakovsky
Copy link
Member

I had an idea of doing the same for pass, but I am not sure whether it's reasonable.

@int-index
Copy link
Member

When comparing

whenJust :: forall m f . (Applicative f, Monoid m) => Maybe a -> (a -> f m) -> f m

and

whenJust :: Monoid m => Maybe a -> (a -> m) -> m

note that mempty and pure mempty need not be equal, so these functions can behave differently for some instantiations of f.

@chshersh
Copy link
Contributor Author

chshersh commented Mar 6, 2018

The reason for having whenJust :: Monoid m => Maybe a -> (a -> m) -> m type is because sometimes we want something like foldMap for Maybe but nobody came up with good name for this function.

@gromakovsky gromakovsky added the type:breaking Breaking change (removal, renaming, semantic change, etc.) label Aug 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request have to think type:breaking Breaking change (removal, renaming, semantic change, etc.)
Projects
None yet
Development

No branches or pull requests

3 participants