Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

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 2 commits into from
Jun 19, 2015

Conversation

ianks
Copy link
Contributor

@ianks ianks commented Jun 8, 2015

Normal try_update now returns without raising an exception while try_update! will raise an exception.

@ianks ianks force-pushed the atomic-ref-try branch from 49e7403 to 7217b6e Compare June 8, 2015 21:51
@ianks ianks force-pushed the atomic-ref-try branch from 7217b6e to 28f89b9 Compare June 8, 2015 21:55
@jdantonio
Copy link
Member

@ianks Thank for working on this! Can you please add a @note to the yardoc indicating the change in behavior? Don't worry about the failing tests. Those tests are on my list (#318).

@jdantonio jdantonio added this to the 0.9.0 Release milestone Jun 9, 2015
@jdantonio jdantonio added enhancement Adding features, adding tests, improving documentation. in progress labels Jun 9, 2015
return unless compare_and_set old_value, new_value

new_value
end
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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.

@ianks
Copy link
Contributor Author

ianks commented Jun 17, 2015

I added a note about the update API.

jdantonio added a commit that referenced this pull request Jun 19, 2015
AtomicReference: update try_update API (resolves #330)
@jdantonio jdantonio merged commit d5b2635 into ruby-concurrency:master Jun 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Adding features, adding tests, improving documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants