-
Notifications
You must be signed in to change notification settings - Fork 416
AtomicReference: update try_update API (resolves #330) #336
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
Merged
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 hidden or 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 hidden or 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 hidden or 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.
We could consider returning
[success, new_value]
because nil cannot be distinguished from new value nil.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.
We should be somewhat consistent with the similar function in
AtomicMarkableReference
. In that class we return an immutable array with[new_val, new_mark]
. We could have both methods return a list with true/false as the first element. Having worked in Erlang for a few years I'm very fond of this pattern. It's very common in Erlang to return a tuple of{ok}
or{error message}
from functions.If we want to go in this direction we should consider using either
ImmutableStruct
orMaybe
. I added the latter specifically for situations like this. Maybe (aka Option) is a common pattern in functional languages and libraries that I'm very fond of but that I haven't seen much in Ruby.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 think that returning
[success, new_value]
might start adding unneccesary verbosity to the API. When you think about it,AtomicMarkableReference
would have to then return,[success, [new_mark, new_value]]
which seems needlessly complex IMO. Java doesn't even add that level of verbosity to the API 😆Another alternative might be to initialize the value with some sort of private
EmptyObject
or something which is used as a placeholdern in order to distinguish fromnil
.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.
In the end, the problem is that idiomatic Ruby abuses
nil
in ways that can make return values ambiguous. That's why I created the obligation API (#value and #reason) methods very early on. It removes the ambiguity. I don't have a strong opinion but I'm comfortable leaving it returningnil
. For a couple of reasons. Mostly because that's the way the API previously worked. I believe that the baseline assumption is that we do not change APIs unless there is a compelling reason to do so. Secondly, returningnil
is idiomatic Ruby, whether we like it or not. Finally, since the user is passing a block to the function it should be clear when looking at the code ifnil
is a valid return value, and the block can be refactored if necessary.I'm going to merge this PR now and we can revisit later if we think there may be a better way to handle this return value.
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.
👍 We could add a note to the documentation though to warn the users. If they need to distinguish
nil
,compare_and_set
can be always used. From my side we've considered[success, new_value]
and it's a bad idea.