Full closures for D

Kevin Bealer kevinbealer at gmail.com
Mon Nov 5 22:50:53 PST 2007


My futurism library has support for the kind of thing you are describing.  Two of
the several syntaxes it supports are shown here; both of these are for reading two
disk files in parallel.

    // Read the contents of two files in parallel:

    char[] read_file(char[] fname)
    {
        return cast(char[]) read(fname1);
    }

    auto f1 = make_future(& read_file, fname1);
    auto f2 = make_future(& read_file, fname2);

    // Get the results - will block until each read() is done.
    char[] fv1 = f1.value;
    char[] fv2 = f2.value;

And this:

    alias Future!(char[]) FString;

    char[] fname1 = "a.txt", fname2 = "b.txt";

    FString f1 = new FString({ return cast(char[]) read(fname1); });
    FString f2 = new FString({ return cast(char[]) read(fname2); });

    // Get the results - will block until each read() is done.
    char[] fv1 = f1.value;
    char[] fv2 = f2.value;

You can find it at dsource.org/projects/futurism.  It's a few more than 7 lines,
but a lot of it is thread pool management and so on.

It took a bit of trial and error and some newsgroup chat to nail down all the
syntax for capturing arguments like the fname1 shown here.  (Maybe some of that
would not be needed now that full closures exist.)

Kevin



More information about the Digitalmars-d mailing list