One more update on d-programming-language.org

bearophile bearophileHUGS at lycos.com
Wed Sep 15 04:31:34 PDT 2010


Seth Hoenig:
> [1] http://live.gnome.org/Vala/ValaForJavaProgrammers

It seems Vala is shaping up into a comprehensive D-like language. Few of the interesting features:

---------------

Vala supports string templates: @"...". String templates may contain expressions, prefixed by a $ sign.

string name = "John";
stdout.printf (@"Welcome, $name!");
stdout.printf (@"3 + 2 = $(3 + 2)");

---------------

Vala: rectangular multi-dimensional arrays [,], [,,], etc. (allocated as one contiguous memory block), jagged array support planned

int[,] matrix = new int[3,4];

---------------

Vala: no implicit inheritance from Object (GLib.Object)

public class Foo : Object {
    // ...
}

What happens if you don't inherit from Object? Nothing terrible. These classes will be slightly more lightweight, however, they will lack some features such as property change notifications, and your objects won't have a common base class. Usually inheriting from Object is what you want.

---------------

Vala: attributes, built into the compiler. Syntax: [AttributeName (param1 = value, param2 = value)]. Mostly used for bindings or D-Bus interfaces. The most prominent attribute for bindings is [CCode (...)]

---------------

Properties

Vala: language support for properties, get {} and set {} blocks, can be accessed like fields

public class Person : Object {
    private int _age = 32;

    public int age {
        get { return _age; }
        set { _age = value; }
    }
}

void main () {
    var p = new Person ();
    p.age++;
}

Or even shorter for the standard implementation:

public class Person : Object {
    public int age { get; set; default = 32 }
}

---------------

Vala: signals (signal keyword, .connect() and .disconnect())

public class MyButton : Object {

    public signal void clicked ();

    public void test () {
        clicked ();          // emit signal
    }
}

void handler_c (MyButton source) }
    stdout.printf ("handler C\n")
}

void main () {
    var b = new MyButton ();
    b.clicked.connect ((s) => stdout.printf ("handler A\n"));
    b.clicked.connect ((s) => {
        stdout.printf ("handler B\n")
    });
    b.clicked.connect (handler_c);
    b.test ();
    b.clicked.disconnect (handler_c);
}

---------------

Property Change Notification

Vala: Subclasses of Object have a notify signal

public class Demo : Object {
    public string title { get; set; }
}

void main () {
    var demo = new Demo ();
    demo.notify.connect ((s, p) => stdout.printf ("Property %s changed\n", p.name));
    demo.title = "hello";
    demo.title = "world";
}

However, you can't get the old value.

If you're only interested in change notifications of a single property you can use this syntax:

demo.notify["title"].connect ((s, p) => stdout.printf ("title changed\n"));

Change notifications can be disabled with a CCode attribute tag immedietely before the declaration of the property:

class MyObject : Object {

    // notify signal is NOT emitted upon changes in the property
    [CCode (notify = false)]
    public int without_notification { get; set; }

    // notify signal is emitted upon changes in the property
    public int with_notification { get; set; }
}

---------------

Nullability

In Vala you must mark reference type parameters of methods as nullable with a question mark (?) if it should be allowed to pass null, e.g.

void my_method (Object? a, Object b) { }

void main () {
    my_method (null, new Object());  // allowed (first argument may be null)
    my_method (null, null);          // not allowed (second argument must not be null)
}

This is checked both at run-time and partially at compile time and helps preventing null pointer dereferencing.

You can enable (experimental) strict nullability checking with --enable-experimental-non-null. Then Vala will check all reference type variables at compile time, not only method arguments. For example, this example won't compile with strict nullability checking enabled:

void main () {
    string? a = "hello";
    string b = a;        // Error: 'a' could be null and 'b' is not nullable
}

However, if you cast the nullable variable into a non-nullable variable with (!) it will compile:

void main () {
    string? a = "hello";
    string b = (!) a;
}

---------------

Additionally a Vala class can have a class construct { } block. This block will be executed once at the first use of its class, and once at the first use of each subclass of this class.

---------------

Bye,
bearophile


More information about the Digitalmars-d mailing list