concept future in category c++

appears as: future, A future, future, futures
Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

This allows you to control which state changes are valid and should be performed, and which shouldn’t. For example, when setting the birthdate of a person, we might want to verify the date isn’t in the future, the person isn’t older than her parents, and so on. This verification reduces the number of states the program can be in to only the states we consider valid. Although this improves the chances of writing correct programs, it still keeps you open to various problems.

You need a handler that will give you access to the data after it becomes available. Let’s call it a future, because the data isn’t available immediately but will become available sometime in the future (see figure 10.15). This idea of future values can be useful for other things as well, so we won’t limit the future to containing only strings (the source of a web page); it will be a generic handler future<T>.

Figure 10.15 Instead of waiting for the slow function to finish, it’s better to have that function return a handler you can use to access the value after it’s calculated.

c10_15.png

To summarize, the handler future<T> may not yet contain a value, but the value of type T will be in it at some point. With it, you’ll be able to design programs that have different components executed concurrently or asynchronously. Any time you have a slow operation, or an operation whose execution time you don’t know, you’ll make it return a future instead of an ordinary value. A future looks like a container type, but a container whose element you can’t get directly—unless the asynchronous operation has finished, and the value is in the container.

Let’s first check whether a future can be a functor. You need to be able to create a transform function that takes future<T1> and a function f: T1 → T2, and it should yield an instance of future<T2>.

Conceptually, this shouldn’t be a problem. A future is a handler to a future value. If you can get the value when the future arrives, you’ll be able to pass it to function f and get the result. At some point in the future, you’ll have the transformed value. If you know how to create a handler for it, you can create the transform function for futures, and the future is a functor.

This gives you a future of the string which, when it arrives, will give you only the title of the web page.

If the future is a functor, it’s time to move to the next step and check whether it’s a monad. Constructing a handler that already contains a value should be easy. The more interesting part is mbind. Let’s change the result type of user_full_name and to_html yet again. This time, to get the full name of the user, you need to connect to a server and fetch the data. The operation should be performed asynchronously. Also, imagine to_html is a slow operation that also needs to be asynchronous. Both operations should return futures instead of normal values:

future<std::string> user_full_name(const std::string& login);
future<std::string> to_html(const std::string& text);

If you used transform to compose these two functions, you’d get a future of a future of a value, which sounds strange. You’d have a handler that will sometime in the future give you another handler that, even later, will give you the value. This becomes even more incomprehensible when you compose more than two asynchronous operations.