Vala Programming/Intro/Getting Started

Getting Vala
As I write this, Vala is still under heavy development, and the most recent version of the vala compiler " " 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:

Hello World
We will begin with the standard Hello World program:

Explanation
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.

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 Run
Save this file as hello.vala. Assuming you have Vala installed, then all it takes to compile and execute this program is:

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 Example
We may write a more complex example showing some of Vala's object oriented features:

Explanation
The line

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 identifies the beginning of a class definition, defining a class called Sample which is a subclass of the  Class from the   namespace.

The line 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).

Prints out the characters "Hello, World", and returns the value zero to the caller, signalling that the function terminated successfully. In case of an error, the main function usually returns 1.