-
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
Conversation
return unless compare_and_set old_value, new_value | ||
|
||
new_value | ||
end |
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
or Maybe
. 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 from 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.
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 returning nil
. 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, returning nil
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 if nil
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.
I added a note about the update API. |
AtomicReference: update try_update API (resolves #330)
Normal
try_update
now returns without raising an exception whiletry_update!
will raise an exception.