This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Programming

Programming GNOME applications with Vala

By Ben Martin on December 10, 2008 (9:00:00 AM)

Share    Print    Comments   

GNOME's Vala programming language lets you use the GLib2 object system at the heart of the GNOME desktop without having to do object-oriented programming in ANSI C. Unlike Mono or Java, a Vala program does not require any virtual machine or runtime libraries, so people who use your Vala objects don't even have to know they are not written in C.

The Vala compiler, valac, compiles Vala code into C code, which is then compiled with gcc into object code. A big issue when you try to use a high-level language in a traditionally C environment is language bindings: where they come from, how well maintained they are, and whether there are bugs in them. Using a high-level language can be more frustrating than just using C if the bindings are not high quality. Vala includes tools that use GLib introspection, which lets you generate a Vala binding for any GLib object. The packages of Vala for Fedora 9 include bindings to GLib2, GTK+2, SDL, SQLite, WebKit, libsoup, libglade-2, hildon, hal, gstreamer, cairo, and dbus -- thus, many of the libraries you would want for a GLib2/GTK+2 based desktop or handheld application are already available to a Vala application. The project also maintains a list of projects that provide Vala bindings.

That takes care of using C code from Vala -- what about the other way around? Vala can produce header files for GLib2 objects so that folks who use C/C++ can easily use your objects. Any language that can work from a C header file describing your GLib2 object should be able to use your Vala objects, so Perl and Python should also be able to use objects implemented in Vala.

Using Glib2 lets you define your own classes with signals and properties. For signals, in a GLib2 program written in C you use g_signal_new in your class_init function to tell GLib2 that this GObject should have a new signal associated with it. This new signal gets a unique numeric identifier, the signalID. To emit a signal you use g_signal_emit, passing the signalID and the parameters, if any, for the signal. Registering a new signal is quite verbose, calling the signal requires use of a numeric signalID, and you're not going to get good static type checking on the line that emits the signal.

The below C code first registers a new signal called "test-signal1" for a GLib2 object. The notify function can be attached to this signal and is used to respond to changes when the test-signal1 is emitted. The g_signal_connect function call ensures that the notify function is called whenever the test-signal1 is emitted. The next line actually emits the signal, causing the notify function to be called.

... class_init() { ... g_test_signals[TEST_SIGNAL1] = g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT); ... } ... static void notify (GObject *object, GParamSpec *spec, gpointer user_data) { gint value; g_object_get (object, "test-prop", &value, NULL); g_print ("+ %d", value); } ... test1 = g_object_new (G_TYPE_TEST, NULL); g_signal_connect (test1, "test-signal1", G_CALLBACK (notify), NULL); ... g_signal_emit (G_OBJECT (test), g_test_signals[TEST_SIGNAL1], 0, 0);

By contrast, the below Vala code defines and uses a Glib2 signal test_signal1. The signature of the signal is the same as the one in the above C example: it takes an integer and returns nothing. Notice that the signal is defined almost like a method; the parameters are defined using the same types as are used in other places in the code instead of the G_TYPE_NONE/G_TYPE_INT macros, and emitting the signal as is done on the last line of the program via a function invocation syntax. The line that includes the append operator for the signal, +=, defines a handler function inline that simply prints the argument to standard output.

public class Test : GLib.Object { public signal void test_signal1(int a); public static void main(string[] args) { Test t1 = new Test(); t1.test_signal1 += (t, a) => { stdout.printf("%d\n", a); }; t1.test_signal1(5); } }

The Vala code is much more succinct, even though it actually does a lot more than the comparable C code. It is the complete Vala source for a runnable program using a custom GLib2 class.

Note that Vala will perform static type checking on the line that the signal is emitted. If I changed that line to try to emit test_signal1 with a string as its argument, the below compilation error would result.

$ valac -o signal signal.vala signal.vala:12.25-12.29: error: Argument 1: Cannot convert from `string' to `int' t1.test_signal1("foo"); ^^^^^ Compilation failed: 1 error(s), 0 warning(s)

The language features of Vala do not stop at just making object and signal definition and handling easier. Vala also supports type inference for local variables, var foo = getFoo();, turning GLib2 errors into exceptions, and using foreach to iterate over a collection instead of the more verbose GLib C API.

Having a language with modern design features like foreach and lambda functions to write GLib2 object-based programs is wonderful. With Vala you can generate header files for your objects, and folks using other languages can use your objects without needing to think about how you have implemented them.

However, while Vala includes features targeted at making writing GLib2 programs as simple and pleasurable as possible, there are few resources for learning, let alone books about, the Vala language.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   

Comments

on Programming GNOME applications with Vala

Note: Comments are owned by the poster. We are not responsible for their content.

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 82.150.225.18] on December 10, 2008 09:44 AM
Why yet another language? Isn't there allready a whole bestiarium of languages C++, Objective C, C#, D, Java. IMHO this is a sign of arogance and ignorance. Every system, when reaching a "production-grade" size, has some bugs and compromises build in. It is naive to think that a new "light weight" macro based domain specific language will share a better destony. I would ask every developer first to think twice about making something, what allready exists, better, before inventing something simmilair for the second time. It will probably have problemms too, just different ones.

#

Re: Programming GNOME applications with Vala

Posted by: Anonymous [ip: 85.34.91.148] on December 10, 2008 10:45 AM
Vala is clearer and more concise than C++. Objective C is barely used in Linux. C# needs a virtual machine (and Vala has a similar syntax, so it could be thought as "compiled C#"). D is a good language, but gdc hasn's been updated for over 1 year, and dmd is not free. Java needs a VM.

Vala was born to ease the development of compiled software under Linux and glib. It has more (linux) bindings than D, and it is easier to write one. It translates to C, so you can use your classes with C (and not only with C++).

#

Re(1): Programming GNOME applications with Vala

Posted by: Anonymous [ip: 198.133.100.133] on December 10, 2008 01:47 PM
Personally I think you're wrong..Vala is a bag of crap and one of the many reasons why I'll be making the switch to KDE. And I'm quite happy they use C++ there.

#

Re(1): Programming GNOME applications with Vala

Posted by: Anonymous [ip: 82.150.225.18] on December 10, 2008 03:03 PM
When You start argumenting with "clearer" and "concise" then I am hearing "small and easy to learn". With time, Vala will get bigger and as dirthy and full with compromises as any other (existing) comparable technology - thats life(cycle). IMHO that is the moment, You will start to look back and wondering if with a little more effort invested in either:
- making compiled Java (gcj) to run smooth with gnome GUI
- integrating gdc seemless into the gcc company
- helping the mono ( -thematic ;-) ) people to speed up the work on their already planed compiler
- looking how to modernise Next Step / Cocoa for linux or integrate it with gnome

You would not get further or travel faster to the future.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 63.229.136.41] on December 10, 2008 11:31 AM
I've used zinc, and gob before, and while they can help in their own way, I usually ended up ditching the them once I had the interface finalized (dubious practice BTW). Mainly because I needed the flexibility that only the straight C source offered.

Vala looks *very* promising (as a Gobject pain reliever / libglade alternative).

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 90.195.40.53] on December 10, 2008 03:27 PM
I think a few of you miss the point of vala.
vala helps you write gobject code, gobject code is such a pain to deal with in stright C and thus the ability to use vala (that compiles to stright C) is a great bonus. you are still free to use whatever the hell language you want, its just a tool to make things easier.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 69.37.30.13] on December 10, 2008 04:22 PM
I don't think most people realize the benefits and reasons for using Vala over other languages. Vala is translated to C and GNOME is written in C which makes it much easier to maintain existing libraries without having to rewrite them or cause confusion with different object systems. No other language could wrap around Glib so neatly. Vala was created with Glib specificly in mind. Vala will make it easy for people to actually code GNOME programs without rewriting the entire underlying system in a new language.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 84.188.186.98] on December 10, 2008 04:41 PM
I am using vala for more than 6 months now. I can really say I really love it. Vala is just a beautyful language. Clear and easy to learn. You can do very complex stuff with it and the code is easier to maintain than C++ code.
.....And it is fast! In many cases faster than C++.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 91.176.139.214] on December 10, 2008 04:49 PM
--
No other language could wrap around Glib so neatly
--
vala helps you write gobject code, gobject code is such a pain to deal with in stright C and thus the ability to use vala (that compiles to stright C) is a great bonus
--

For me the problem is not Vala... the problem is GNOME and it's libs that are not well coded and hardly usable compared to say.. Qt... Ditch GNOME, GTK and Glib and use something that was well thought from the beginning, and easy to maintain.

#

Re: Programming GNOME applications with Vala

Posted by: Anonymous [ip: 84.188.186.98] on December 10, 2008 05:14 PM
Nobody forces you to use vala or gnome libraries. You can stay with Qt and C++.

#

Re: Programming GNOME applications with Vala

Posted by: Anonymous [ip: 69.37.30.13] on December 10, 2008 05:25 PM
What reason does GNOME have to ditch GTK and C in favor of QT and C++? Becaues you don't like it? There is already a QT based desktop for linux, stay there if you don't like GNOME. I just don't understand why anyone would expect developers who have worked hard for years on a project to ditch it entirely based on some random users complaints. Some people will never like C++ and some will never go back to plain old C. Deal with it. No one is forcing anyone to use one language over another. Your argument about GNOME's libs being "hardly usable" is not a consensus sentiment.

#

Simple and pleasurable? With no documentation??

Posted by: Anonymous [ip: 82.192.250.149] on December 10, 2008 05:56 PM
"while Vala includes features targeted at making writing GLib2 programs as simple and pleasurable as possible, there are few resources for learning, let alone books about, the Vala language."

Yet another poorly documented language. And therefore a complete waste of time as far as I'm concerned.

If somebody REALLY wants to help developers, the most important task is to clean up the documentation of something that's already out there. Like GTKmm, Ruby, .........

But of course it's much more "fun" to develop a useless new language. This is the curse of free software.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 91.128.187.16] on December 10, 2008 08:00 PM
I thought about starting some programming as i am a physics student and i will need some experience somewhen. I only know some turbopascal and have a basic understanding about functions etc. I am just considering c or c++, as anything other than that would be too slow. But then i saw some vala benchmarks on mandelbrot and similar simulations and it's very close to c in speed. The only problem is, to learn c, I got some free ebook, thats pretty good. But i couldn't find any ebook for vala(anything other than an ebook is useless imho, as there is no red line through the text). So if anyone knows a source, please tell me(doesn't have to be too long, some 50 or 100 pages are enough usually).

Greetings

#

Re: Programming GNOME applications with Vala

Posted by: Anonymous [ip: 72.255.88.212] on January 06, 2009 03:52 AM
Vala will not compare to C++ in speed unless you fight the grain of the language. Nearly everything is done through virtual calls and so cannot be inlined. There are no expression templates so math will involve a lot of copying and function calling. You "can" write really fast code in Vala but it wont be modular since the mode of modularity is the function call. All generics in Vala look up the function like object->interface->function. That's two indirections. Private members are also accessed like object->priv->x, with another indirection just to keep the C code clean. It's a great tool and it's faster than Python but it's not really meant for speed. As far as learning Vala, just look at the vala compiler source code since it's written in vala. If that doesn't make any sense then learn Ruby or Python first as a learning exercise.

#

Programming GNOME applications with Vala - the point

Posted by: Anonymous [ip: 72.75.157.31] on December 10, 2008 10:33 PM
Vala directly uses the native gobject model of GNOME, making it come alive, can directly use other libraries, and requires no extra runtime overhead wrapper libraries. Simply put, while one can potentially write GNOME apps in C# (mono), or Java, each of these require BOTH mapping of the object model and wrappers for native methods and for accessing all the various GNOME and other libraries. And of course, both require extension vm runtime support as well.

As for C++, you need libgtkmm, libglademm, and libgnomemm, to wrap and support the object model in C++ classes, and other libraries.

Dynamic languages like python, perl, etc? Runtime performance issues depending on what your trying to do as well as again the need for bindings.

If one is writing purely GNOME applications, and especially small applications, or other things that expect to use gtk, potentially vala will clearly be far better than any of the above choices. What is lacks is a gcc front-end so that rather than emitting c code and using an external compiler, it can directly produce binaries that have dwarf meta information so it can be reasonably debugged directly back to the original vala source files.

#

Programming GNOME applications with Vala

Posted by: Anonymous [ip: 81.149.173.98] on December 12, 2008 09:39 AM
As a programmer of 40 years standing, who knows absolutely nothing about Vala except what I read here, I have only one thing to say.
A programming language lives or dies according to the quality, understandability, and thoroughness of its documentation; documentation for the programmer in how to use it, and the culture of its programmers in how they document their code (which is helped by what documentation tools are available).
If you have "few resources" to teach programmers how to use it, that says two things: you either don't understand or you don't prioritise properly; and your language is doomed.

#

Vala documentation

Posted by: Anonymous [ip: 69.37.169.161] on December 13, 2008 01:12 AM
Vala does have documentation. It isn't complete but neither is the language. Vala isn't even 1.0 yet. The existing documentation is good enough for any programmer to get started though. The syntax isn't anything esoteric, it's pretty standard fare. Like a previous comment stated, the real beauty of Vala is it's native mapping to GNOME libraries. I feel bad for the GNOME developers because it seems like there are warring factions of language nazis that either do or don't want mono, python, vala, java, or plain old C as the default language for developing applications. I think Vala will win out though because it doesn't have any of the overhead of any of the VM languages or scripting languages, maps very neatly to the GNOME libraries, and has near C speed.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya