Skip to content

Added :args to Future and Promise options hash. #224

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 1 commit into from
Jan 25, 2015

Conversation

jdantonio
Copy link
Member

This PR addresses the enhancement discussed in #223. It adds an :args option to the constructor and factory of Future and Promise. The value of this option, when set, can be either a single value or an array of zero or more values. When the args option is given the values will be passed to the underlying executor and, subsequently, passed to the task block. Providing :args isolates the scope of the variables so that they can be re-assigned within the closing scope. The passed variables are still mutable object references that can be modified within the task block.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling ad10456 on refactor/future-args into 1241953 on master.

@jdantonio
Copy link
Member Author

The following examples now work as expected:

require 'concurrent'

value = 100
future = Concurrent::Future.execute(args: value) do |t_value|
  sleep 1
  t_value + 1
end
value = 200
p future.value #=> 101


value = (1..10).to_a
future = Concurrent::Future.execute(args: value) do |*t_value|
  sleep 1
  t_value << 1000
end
value = nil
p future.value #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1000]

@coveralls
Copy link

Coverage Status

Coverage increased (+0.09%) when pulling 6bd5b08 on refactor/future-args into 1241953 on master.

@jrochkind
Copy link
Contributor

makes sense to me!

def execute
if compare_and_set_state(:pending, :unscheduled)
@executor.post{ work }
@executor.post(@args){work }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: lost space

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! :-)

@pitr-ch
Copy link
Member

pitr-ch commented Jan 16, 2015

LGTM

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) when pulling a9dd938 on refactor/future-args into 1241953 on master.

@jdantonio jdantonio added this to the 0.8.0 Release milestone Jan 25, 2015
@jdantonio jdantonio force-pushed the refactor/future-args branch from a9dd938 to e3e0501 Compare January 25, 2015 14:52
@coveralls
Copy link

Coverage Status

Coverage increased (+7.18%) to 91.77% when pulling e3e0501 on refactor/future-args into 474bdf6 on master.

jdantonio added a commit that referenced this pull request Jan 25, 2015
Added :args to Future and Promise options hash.
@jdantonio jdantonio merged commit 202766b into master Jan 25, 2015
@jdantonio jdantonio deleted the refactor/future-args branch January 25, 2015 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants