Last but not least, we can find goodies in the PPL, which are futures. A future can be defined as a value that we already know we'll need in the future. The key point is getting (calculating, building, retrieving, and so on) a value that is expensive in terms of computation resources (CPU, time, network, and so on), and that we don't want to affect the main/UI thread with this burden.Â
The following code shows the implementation of a future:
var
LFuture: IFuture<Integer>;
begin
LFuture := TTask.Future<Integer>(
function : Integer
begin
Sleep(5000); // Do something...
Result := 42;
end
);
ShowMessage(LFuture.Value.ToString);
end;
The previous code example shows how a future is defined. Basically, we provide a builder function that is capable of returning our value and we wrap this function in a task.
The future implementation relies on another modern language feature—generics. We are trying...