Vala (programming language)

From Wikipedia, the free encyclopedia

Vala
Paradigm structured, imperative, object-oriented
Appeared in 2006
Developer Jürg Billeter, Raffaele Sandrini
Influenced by C, C++, C#, Java

Vala is a new (as of 2006) programming language, targeting the GObject object system. It was developed by Jürg Billeter and Raffaele Sandrini.

It has a syntax based upon C#. However, unlike C#, it has no runtime system (Vala is compiled to C code, which can then be compiled to native code) and so imposes little additional overhead.

The primary advantage of Vala over C++/gtkmm is that Vala is memory-managed (not using garbage collection).

[edit] Code example

The "Hello World" program:

using GLib;
 
class Sample : Object {
        void run () {
                stdout.printf ("Hello World\n");
        }
 
        static int main (string[] args) {
                var sample = new Sample ();
                sample.run ();
                return 0;
        }
}

The generated C code (header and source file):

#ifndef __SAMPLE_H__
#define __SAMPLE_H__
 
#include <glib.h>
#include <glib-object.h>
 
G_BEGIN_DECLS
 
 
 
 
G_END_DECLS
 
#endif
#include "Sample.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct _Sample Sample;
typedef struct _SampleClass SampleClass;
typedef struct _SamplePrivate SamplePrivate;
 
#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))
 
struct _Sample {
        GObject parent_instance;
        SamplePrivate * priv;
};
struct _SampleClass {
        GObjectClass parent_class;
};
enum  {
        SAMPLE_DUMMY_PROPERTY
};
static void sample_run (Sample* self);
static gint sample_main (int args_length1, char** args);
static Sample* sample_new (void);
static gpointer sample_parent_class = NULL;
static GType sample_get_type (void);
 
 
static void sample_run (Sample* self) {
        g_return_if_fail (IS_SAMPLE (self));
        fprintf (stdout, "Hello World\n");
}
 
 
static gint sample_main (int args_length1, char** args) {
        Sample* sample;
        gint _tmp0;
        sample = sample_new ();
        sample_run (sample);
        return (_tmp0 = 0, (sample == NULL ? NULL : (sample = (g_object_unref (sample), NULL))), _tmp0);
}
 
 
int main (int argc, char ** argv) {
        g_type_init ();
        return sample_main (argc, argv);
}
 
 
static Sample* sample_new (void) {
        Sample * self;
        self = g_object_newv (TYPE_SAMPLE, 0, NULL);
        return self;
}
 
 
static void sample_class_init (SampleClass * klass) {
        sample_parent_class = g_type_class_peek_parent (klass);
}
 
 
static void sample_init (Sample * self) {
}
 
 
static GType sample_get_type (void) {
        static GType sample_type_id = 0;
        if (G_UNLIKELY (sample_type_id == 0)) {
                static const GTypeInfo g_define_type_info = { sizeof (SampleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sample_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Sample), 0, (GInstanceInitFunc) sample_init };
                sample_type_id = g_type_register_static (G_TYPE_OBJECT, "Sample", &g_define_type_info, 0);
        }
        return sample_type_id;
}

[edit] External links