Vala (programming language)
Paradigm | structured, imperative, object-oriented |
---|---|
Developer | Jürg Billeter, Raffaele Sandrini |
First appeared | 2006 |
Stable release | 0.5.7
/ February 20, 2009 |
OS | Every platform supported by GLib |
License | LGPL 2.1+ |
Website | http://live.gnome.org/Vala |
Influenced by | |
C, C++, C#, Java |
Vala is a programming language that tries to bring modern language features to C, without additional runtime requirements and with little overhead, by targeting the GObject object system. It was developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#. Rather than being compiled directly to assembler or to an intermediate language, Vala is compiled to C which is then compiled with the platform's standard C compiler.
For memory management the GObject system provides Reference counting. In C the programmer must manage the addition and removal of references manually but in vala the management of these reference counts is automated (provided the programmer uses the language's built-in reference types rather than plain pointers).
To use functionality from native code libraries requires writing vapi files that define the interface to the library. Vapi files are provided for many of the glib/gtk/gnome libraries.
Code example
A simple "Hello World" program:
void main () {
print ("Hello World\n");
}
A more complex version, showing some of Vala's object-oriented features:
class Sample : Object {
void run () {
stdout.printf ("Hello World\n");
}
static void main (string[] args) {
var sample = new Sample ();
sample.run ();
}
}
Generated C code
The generated C code for the last example (assuming it is held in a file named tmp.vala) is:
// tmp.h
#ifndef __TMP_H__
#define __TMP_H__
#include <glib.h>
#include <glib-object.h>
G_BEGIN_DECLS
#define TYPE_SAMPLE (sample_get_type ())
#define SAMPLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SAMPLE, Sample))
#define SAMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SAMPLE, SampleClass))
#define IS_SAMPLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SAMPLE))
#define IS_SAMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SAMPLE))
#define SAMPLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SAMPLE, SampleClass))
typedef struct _Sample Sample;
typedef struct _SampleClass SampleClass;
typedef struct _SamplePrivate SamplePrivate;
struct _Sample {
GObject parent_instance;
SamplePrivate * priv;
};
struct _SampleClass {
GObjectClass parent_class;
};
Sample* sample_construct (GType object_type);
Sample* sample_new (void);
GType sample_get_type (void);
G_END_DECLS
#endif
// tmp.c
#include "tmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
SAMPLE_DUMMY_PROPERTY
};
static void sample_run (Sample* self);
static void sample_main (char** args, int args_length1);
static gpointer sample_parent_class = NULL;
static void sample_run (Sample* self) {
g_return_if_fail (self != NULL);
fprintf (stdout, "Hello World\n");
}
static void sample_main (char** args, int args_length1) {
Sample* sample;
sample = sample_new ();
sample_run (sample);
(sample == NULL) ? NULL : (sample = (g_object_unref (sample), NULL));
}
int main (int argc, char ** argv) {
g_type_init ();
sample_main (argv, argc);
return 0;
}
External links
- The Vala Programming Language, on GNOME Live!
- Benchmark of Vala versus C# and C
- The Vala Programming Book