Re: [Vala] [ANNOUNCE] Vala 0.5.1 - Compiler for the GObject type system



On Mon, 2008-11-03 at 16:42 -0800, Christian Hergert wrote:

        Changes since 0.4.0
         * Support type checks for error domains and error codes.
         * Experimental support for yield statements and coroutines.
         * Support GValue and GHashTable in D-Bus clients.
         * Various improvements to the .gir reader.
         * Drop deprecated support for static classes.
         * Modularize code generator.
         * Many bug fixes.

Are there any examples of the yield and co-routine syntax?

Sure, but please bear in mind that it is still experimental and only
partially implemented. A simple stand-alone example:

void foo () yields {
        message ("hello");
        yield;
        message ("world");
}

void main () {
        foo.begin ();
        message ("vala");
        
        var loop = new MainLoop (null, false);
        loop.run ();
}

This will print the messages in the order "hello" "vala" "world".
The yield statement in the method foo() schedules the rest of the method
to be run in an idle handler. The invocation of foo() in main using
`begin' lets the method return on the first yield instead of waiting for
it to finish.

While using the idle handler to split a large computation in multiple
parts may help in some cases, the more interesting use case is async
I/O.

void enumerate_files (File dir, Cancellable? cancellable) yields {
    try {
        var enumerator = dir.enumerate_children ("*", 0, cancellable);
        while (true) {
            var files = enumerator.next_files (100, cancellable);
            if (files == null) {
                break;
            }
            foreach (FileInfo file_info in files) {
                message ("File: %s", file_info.get_name ());
            }
        }
    } catch (IOError error) {
        warning ("Error: %s", error.message);
    }
}

void main () {
    File dir = File.new_for_path (Environment.get_home_dir ());
    enumerate_files.begin (dir, null);

    var loop = new MainLoop (null, false);
    loop.run ();
}

This example does not yet work but is expected to work in the future. It
lists all files in the home directory but does so asynchronously without
ever blocking the main loop. As you can see it's almost as easy as
performing the same operation synchronously, including error handling.

I'm also planning to support the yield statement to return values in
generators as is already possible in C#, but that's a completely
different use case.

Jürg




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]