-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add when and unless to OptionT #3233
Merged
LukaJCB
merged 2 commits into
typelevel:master
from
strong-zero:add-optiont-when-and-unless
Jan 2, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the implementation of the
else
branch surprising. I would have thought that it is justOptionT.none[F, A]
ifcond
isfalse
(analogous toOptionT.when
) but the current implementation runs theF
-effect and then replaces theA
withNone: Option[A]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems odd and it works when F = List and fa = List.empty because we want to getOptionT(List.empty)
notOptionT(List(None))
. But it breaks the caseOptionT.whenF[List, Int](false)(List(1,2))
because we haveOptionT(List(None, None))
notOptionT(List.empty)
😱I will create a follow-up pull request to fix it.I find no easy "fix" 😱whenF
usesliftF
ifcond
is true.liftF
isOptionT(F.map(fa)(Some(_)))
so ifcond
is false, I think it should use something similar toliftF
so that some "good" properties hold:I should have implemented the case when F assumes the empty value or F is a Monoid separately.Any comments and suggestions are welcome!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these properties imply that
val launchMissiles: IO[Unit] = ...; OptionT.whenF(cond)(launchMissiles)
always launches the missiles, no matter whatcond
is, then I think we should get rid of either these properties orwhenF
. I would prefer the former becausewhenF
seems like a useful utility to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @fthomas. Good catch.
How about this for
whenF
? Many of theF
methods on transformers are specified in terms ofId
, and passes with Frank's proposed change:We might even relate it to
.sequence
for less trivial effects:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fthomas @rossabaker Thank you both for your helpful examples. I will make a follow-up pull request.