cats icon indicating copy to clipboard operation
cats copied to clipboard

Add ifTrueM, ifFalseM, ensureTrue and ensureFalse

Open geirolz opened this issue 1 year ago • 3 comments

This PR adds ifTrueM and ifFalseM for Monad and ensureTrue and ensureFalse for MonadError.

The reasons why they are under Monad and not Applicative is because they needs pure for the unhandled branch.

Considerations

  • ifTrueM / ifFalseM - Idk if this should stay in Monad or just in the syntax since depends on Monoid.
  • ensureTrue / ensureFalse - I was unsure about the naming between these and raiseWhenTrue/raiseWhenFalse.
  • I also thought to add these methods into mouse instead but I ended up that those cases are too common and useful to stay in mouse ( there are also other methods in mouse that IMHO are worth moving to cats, but that is another topic )

Let me know what do you think!

geirolz avatar Jul 19 '24 13:07 geirolz

I'm concerned with Monoid being used just to get a default value and not due to any laws about monoid relating to the functions using it.

johnynek avatar Jul 19 '24 16:07 johnynek

I'm concerned with Monoid being used just to get a default value and not due to any laws about monoid relating to the functions using it.

I see you point - I don't have strong options here. The alternative is to return Unit for both branches.

geirolz avatar Jul 19 '24 17:07 geirolz

The alternative is to return Unit for both branches.

Just for reference, to keep stuff connected: #4626

I also think that instead of ifTrueWhatever and ifFalseWhatever we should use whenWhatever and unlessWhatever – it would be more consistent with other parts of both Cats and Mouse.

satorg avatar Jul 19 '24 19:07 satorg