Skip to content
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

future<T>.always([]{}); #266

Open
aaronalbers opened this issue Jun 13, 2019 · 4 comments
Open

future<T>.always([]{}); #266

aaronalbers opened this issue Jun 13, 2019 · 4 comments

Comments

@aaronalbers
Copy link
Contributor

I think it would be nice to have a way to easily request that a continuation in a chain be done every time regardless of error state. I am accomplishing this now with f.recover([](auto f){}); but I think f.always([]{}); is more expressive.

@FelixPetriconi
Copy link
Member

I would consider at this point, that your tasks do not throw.
We are currently thinking about changing the recover() interface. Perhaps this eases the usage a bit.

@aaronalbers
Copy link
Contributor Author

My tasks certainly do throw but there are instances where regardless of if a task succeeds or not I want some code to execute. This could be useful in many cases but the main one I use it for is state tracking via a construct like RAII. Like when a task finishes do this even if you failed.

I am curious however what the new interface will ultimately turn out to be. I have been following your progress on error and exception.

@dabrahams
Copy link
Contributor

Two questions:

  1. Should .always really be spelled .finally?
  2. Is the closure ever something that is unknown at the point the task is created, or should it just be folded into the task body?

@sean-parent
Copy link
Member

I'm not certain I want to promote the idea of depending on side-effects with future continuations. But I believe (except for the member function syntax) this is easily expressible as:

// arguments are future, executor, task.
template <class T, class E, class F>
auto always(future<T>&& x, E&& e, F&& f) {
    return future<T> move(x).recover(std::forward<E>(e), [_f = std::forward<F>(f)](auto&& f) {
        std::move(_f)();
        return std::forward<F>(f); // is this forward redundant?
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants