Chris Umbel

Easy GNOME Development with the Vala Programming Language

GNOMEI've generally not had a reason to work with the GObject type system despite appreciating its fruits through GNOME for years. Then the other day I ran across a language called Vala which intrigued me enough to start hacking away.

Vala's claim to fame is that it simplifies GObject development by exposing it in a C#/Java like language. Unlike C# and Java Vala is translated to C and then compiled to a native binary. Presumably this leads to performant execution and a tight memory footprint compared to CLI and Java bytecode.

The GObject type system and Vala are new to me so I'm in no position to kick knowledge, but I'll share some of what I've written early in my learning process.

Example 1 Hello World:

using GLib;

public class HelloWorld : Object {
       public void run() {
              stdout.printf("Hello World\n");

       public static int main(string[] args) {
              HelloWorld hellower = new HelloWorld();
              return 0;

producing the output:

Hello World

Example 2 Getting Twitter Status XML:

using GLib;

public class Twitter {
  static int main (string[] args) {
    /* get the username from the command line */
    string username = args[1];

    /* format the URL to use the username as the filename */
    string url = "".printf(username);

    stdout.printf("Getting status for %s\n".printf(username));

    /* create an HTTP session to twitter */
    Soup.SessionAsync session = new Soup.SessionAsync();
    Soup.Message message = new Soup.Message ("GET", url);

    /* send the HTTP request */

    /* output the XML to stdout */

    return 0;

This example will retrieves a twitter user's status via the REST API and outputs the XML response to the console. The Soup library is employed for the HTTP communication.

Example 3 XML parsing and XPath queries:

/* parse the xml into a document object */
Xml.Doc* status_doc = Parser.parse_memory(,

/* create the basic plumbing for XPath */
XPathContext* xpath = new XPathContext(status_doc);

/* execute an xpath query */
XPathObject* result = xpath->eval_expression("/user/status/text");

/* slap the result in a string */
string status = result->nodesetval->item(0)->get_content();

stdout.printf("%s\n", status);

The above code could be grafted into example 2 (requires slapping in a "using Xml;" directive) and would actually pull the status out of the XML response.


Vala is a young language, but an interesting one. It certainly seems like it could make native GNOME development a bit more accessible to C#/Java developers.

A few things that justify it's use over C++ that I haven't covered here are its support for "modern" features such as assisted memory management, the foreach construct, and exception handling.

Thu Jul 16 2009 17:36:47 GMT+0000 (UTC)

Follow Chris
RSS Feed