Last modified on 4 December 2012, at 12:28

Vala Programming/Intro/Getting Started

Vala Programming/Header

Getting ValaEdit

As I write this, Vala is still under heavy development, and the most recent version of the vala compiler "valac" may be downloaded from the vala homepage, and will have to be compiled manually.

It may also be downloaded from your distribution's repositories. On a Debian based distribution (eg: Ubuntu), it may be installed by using the command:

$ sudo apt-get install valac

Hello WorldEdit

We will begin with the standard Hello World program:

void main(string[] args) {
    print("Hello, World\n");
}

ExplanationEdit

void main(string[] args) {

This is the start of a function definition. Vala looks for a function or a method called "main", which will be run when the program starts.

print("Hello, world!\n");

This line instructs Vala to execute the function "print", with a single string argument, "Hello, world!\n". In Vala, this is always the syntax you use to call functions.

The last line simply ends the definition of the main method.

To RunEdit

Save this file as hello.vala. Assuming you have Vala installed, then all it takes to compile and execute this program is:

$ valac hello.vala
$ ./hello
Hello, world!
$

valac is the Vala compiler, which will convert your Vala code into C. It can also automate the whole process of compiling the C into machine code. The resulting binary can then be directly executed on the machine.

If you give valac the -C switch, it will also create two files called hello.h and hello.c. If you look at the content of these files you can see that programming a class in Vala is equivalent to the same task in C, but a whole lot more succinct. You will also notice that this class is defined dynamically in the running system. This is a good example of the power of the GNOME platform, but as I've said before, you do not need to know much about this to use Vala.

A More Complex ExampleEdit

We may write a more complex example showing some of Vala's object oriented features:

    using GLib;
    public class Sample : GLib.Object {
        public static int main(string[] args) {
            stdout.printf("Hello, World\n");
            return 0;
        }
    }

ExplanationEdit

The line

    using GLib;

Informs the compiler that the program will be using the GLib namespace. This namespace is usually imported by default, so it is optional to import it explicitly.

The next line

    public class Sample : GLib.Object {

identifies the beginning of a class definition, defining a class called Sample which is a subclass of the Object Class from the Glib namespace.

The line

     public static int main(string[] args) {

is the definition of a public (visible outside the class) static (may be called without possessing an instance of the class) function that will return an integer value. It is the main entry point of the program (first function called when running the program).

     stdout.printf("Hello, World\n");

Prints out the characters "Hello, World", and

     return 0;

returns the value zero to the caller, signalling that the function terminated successfully. In case of an error, the main function usually returns 1.