What's Vala?
Vala is a new programming language based on C# syntax which compiles to C code rather than Common Intermediate language. Consequently, when the C code is compiled, its performance is comparable to that of a 'normal' C application rather than that of .NET or Mono.
Vala is open source software which is being developed as part of the GNOME platform and has not yet reached version 1.0. The latest version (as I write this) is 0.11.6.
It uses the GObject and GLib libraries and is therefore an alternative to languages such as C++ or Objective C for adding object oriented extensions to the C language.
How does it differ from C#?
The main differences are as follows:
- Instead of garbage collection, Vala uses 'reference counting' which is the same system as COM. This means that a count is kept of all the references to an object and, when this count reaches zero, the object is destroyed. To avoid reference cycles, weak references can be used. It's also possible to destroy objects deterministically by calling the class's destructor.
- Vala uses 'checked exceptions' which is the same approach as Java. This means that a method has to declare all the possible exceptions which it can throw.
- Vala does not support method overloading. This is for compatibility with C which doesn't have this feature either. In practice, this is not as onerous as it sounds because Vala does support optional arguments.
- Interfaces in Vala can contain non-abstract methods and private methods which enables you to create 'mixins'.
- As in Java, enums in Vala can have methods and not just constants.
- Method parameters and return types in Vala are non-nullable by default. So, if you have a variable of type Foo, then it can never be null but a variable of type Foo? can be. This means that you don't usually need to check for null return values.
- Automatic properties in Vala can have a default value as part of their declaration.
- Methods in Vala support 'ensures' and 'requires' clauses for argument and return value checking. In C# you can do something similar using Code Contracts rather than as a built in language feature.
- Pointers can be used anywhere in Vala. In C# they can only be used in 'unsafe' blocks.
- As in C, there's no bounds checking for Vala arrays. In C# bounds checking can only be circumvented by using unsafe code.
- Generics in Vala do not support constraints.
- Surprisingly, Vala already has support for asynchronous calls (using the async/yield keywords) which is planned for C# 5.0 (using the async/await keywords).
- Vala doesn't support LINQ or extension methods.
- There's no operator overloading in Vala.
- Methods in Vala can be global - unlike C#, they don't have to be declared inside classes or structs.
- Vala strings use UTF-8 encoding rather than Unicode.
Does Vala only work on Linux or other Unix based systems?
No, it will work on Windows as well.
To develop Vala applications you can either use MonoDevelop (Mono's IDE), Val(a)IDE which is an IDE written in Vala itself or a text editor and the command line.
How do you write 'Hello World' in Vala?
As a slight departure from the norm, we'll write C# Corner, with alternative letters separated by spaces, to the console:
void main()
{
string s = "C# Corner";
PrintWithSpaces(s);
}
void PrintWithSpaces(string s)
{
for(int i = 0; i < s.length; i++)
{
stdout.printf("%c ", s[i]);
}
}
If you're developing from the command line, you'd save this file as ccc.vala and then compile it with:
valac ccc.vala
Notice that this not only invokes the Vala to C compiler but the C to native code compiler as well to produce ccc.exe
To run it simply type:
ccc
Where can I find more details?
Check out the Vala pages at GNOME (
http://live.gnome.org/Vala) which, as well as documentation, contains links to download the latest version.
As Vala is still being developed, the information in this article may change before the final release.