Signals in Vala

Signals are very easy to use in Vala. They are declared like a method with the special keyword signal. Other methods can add handlers to signals by simply adding (+=) them to the signal. Marshallers for signals with "non-standard" arguments are created automatically.

Example

/* Vala signal example */
using GLib;

class Maman.Foo : Object {
        /* signal declaration */
        public signal void activated (string s);

        public void do_action () {
                /* here the signal is emitted */
                activated ("Hello World");
        }
}

class Maman.Bar : Object {
        public void run () {
                var foo = new Foo ();
                
                /* here we add a hander to the signal */
                /* you can, of course, use lambda functions as signal handler */
                foo.activated += (foo, s) => {
                        stdout.printf (s);
                };
                
                foo.do_action ();
        }

        static int main (string[] args) {
                var bar = new Bar ();
                bar.run ();
                return 0;
        }
}

Compile

$ valac -o signal signal.vala

Vala/Signals (last edited 2009-05-05 18:03:52 by JavierJardon)