Why Vala is the Answer to Life, the Universe, and Everything

I have Vala’s number and I can safely report it is 42.  I’m hardly the first person to notice that Vala rocks but in one area in particular it is so fantastic, so groundbreaking, so amazing I am compelled to write about it.

It is no secret that GObject brings many benefits to the table.  It adds a full C ABI compatible object system to C, no mean task.  It has powerful support for signals built into it’s very core and supports much of the OO functionality one is likely to need.  Because of it’s OO nature and compatability there are many powerful library bindings available which is one of the major strengths of the GNOME platform.

No one ever said GObject was fun to use though.

In fact GObject has a pretty rough reputation especially if you aren’t using those aforementioned powerful library bindings which hide all the ugliness.  This, of course, really isn’t GObject’s fault.  It is not an extension to a C compiler, it is not a preprocessor, it is simply a library and so it can’t add any syntax to facilitate all this.

Ever wonder why some GObject based libraries use lots of access methods and not GObject properties?  Properties are a pain!  You need to create all the properties using GParamSpec, then you have to install them during class initilization.  Don’t forget you have to override the base Gobject get/set methods and provide your own methods to actually set and get these things.  Whenever you want to add a new property you have to touch the code in at least 4 places.  And don’t get me started on creating your own signals.  Signals are much the same but if you want your signal to return a value or take more than a single parameter its time to start generating your marshallers (if you ever wondered what glib-genmarshal does, start creating signals and you’ll find out soon!).

None of this is very hard.  It’s just tedious boilerplate.  Vala makes this all go away.

Vala is amazing because it allows people to easily and quickly utilize the full power of GObject and since your Vala code compiles down to GObject/C anyway you keep all of the compatibility that GObject provides.    Ever wonder what lots and lots of properties definitions would look like in GObject?  Well here is a bit of a Vala class I have:

    public bool HasSheet { public get; private set; }
    public bool Visible { public get; public set; }
    public bool Animated { public get; private set; }
    public float Alpha { public get; public set; }
    public float Rotate { public get; public set; }
    public float Scale { public get; public set; }

    <snip>

And here is a snippet of the generated GObject/C (just the installation!)

g_object_class_install_property (G_OBJECT_CLASS (klass), SPRITE_HAS_SHEET,
g_param_spec_boolean ("HasSheet", "HasSheet", "HasSheet", FALSE,
G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | 
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (G_OBJECT_CLASS (klass), SPRITE_VISIBLE,
g_param_spec_boolean ("Visible", "Visible", "Visible", FALSE,
G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB
| G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (G_OBJECT_CLASS (klass), SPRITE_ANIMATED,
g_param_spec_boolean ("Animated", "Animated", "Animated", FALSE,
G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB
| G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (G_OBJECT_CLASS (klass), SPRITE_ALPHA,
g_param_spec_float ("Alpha", "Alpha", "Alpha", -G_MAXFLOAT, G_MAXFLOAT, 0.0F,
G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB
| G_PARAM_READABLE | G_PARAM_WRITABLE));

<snip>

Vala provides alot more than simply acting as a shorthand for GObject but for someone like me who has traditionally eschewed the language bindings and developed in GObject/C it is mana from heaven.

Comments are closed.