Temporal Programming

Have you ever wanted to know information beforehand?

With temporal programming, you can!

For instance, when reading an input stream, you want to show a progress bar for how far along you’ve come. But, in order to know what percentage of the stream has been processed, you need to know how many bytes it contains.

Some streams are based on files so you can use the file to figure out the size. The same goes for some streams from web servers. But then there are other streams where the size isn’t known beforehand.

Temporal programming was invented for them.

Here’s an example:

temporal long streamBytes;
final ProgressBar progressBar =
  new ProgressBar("Reading stream",
  streamBytes);
long totalBytes = 0;

try (final InputStream is = ...) {
  byte[] buffer = new byte[1024];
  int read;

  while ((read = is.read(buffer)) > 0) {
    totalBytes += read;   

    // do something with bytes
    // ...

    // increase progressbar
    progressBar.step(read);
  }

  // set the max of the progressBar
  streamBytes = totalBytes;
}

All the magic is in the temporal keyword… much like the native keyword, it makes “stuff” happen behind the curtain…

At first, the value of the temporal variable streamBytes isn’t known, and in normal cases, this would create a compile error. However, the value of streamBytes is set at the end of the program, and can then be used in the beginning much as if the variable had been set from the start.

Pretty cool!

Of course, this example with a progress bar and a stream is much less exciting than an example with a stock ticker, for instance…

There’s money to make there!


Header image: By fdecomiteTunnels of Time, CC BY 2.0, Link

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.