Vala Programming/Intro/Programming Style
Hello World
editusing GLib;
public class Test.HelloObject : GLib.Object {
public static void main (string[] args) {
stdout.printf ("Hello, World");
}
}
Explanation
editusing GLib;
A using line informs the compiler that this file is going to be referring to things in the namespace given, and therefore allows them to be used without giving their fully qualified name. The GLib namespace is imported by default, so this line is optional.
public class Test.HelloObject : GLib.Object {
This line identifies the beginning of a class definition. Classes in Vala are very similar in concept to other languages. A class is basically a type of object, of which instances can be created, all having the same properties. The implementation of classed types is taken care of by the gobject library, but details of this are not important for general usage.
What is important to note is that this class is specifically described as being a subclass of GLib.Object. This is because Vala allows other types of class, but in most cases, this is the sort that you want. In fact, some language features of Vala are only allowed if your class is descended from GLib's Object.
Other parts of this line show namespacing and fully qualified names, although these will be explained later.
public static void main (string[] args) {
This is the start of a method definition. A method is a function related to a type of object that can be executed on an object of that type. The static method means that the method can be called without possessing a particular instance of the type. The fact that this method is called main and has the signature it does means that Vala will recognise it as the entry point for the program.
stdout.printf ("Hello, World");
stdout is an object in the GLib namespace that Vala ensure you have access to whenever required. This line instructs Vala to execute the method called printf of the stdout object, with the hello string as an argument. In Vala, this is always the syntax you use to execute a method on an object, or to access an object's data.
The last lines simply end the definitions of the method and class.
To Run
editAssuming you have Vala installed, then all it takes to compile and execute this program is:
$ valac -o hello hello.vala
$ ./hello
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, which is what supplying the -o switch requests. The resulting binary can then be directly executed on the machine. You can probably guess the output.
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.