The syntax of Vala is similar to C#, modified to better fit the GObject type system. Vala supports modern language features as the following:
- Interfaces
- Properties
- Foreach
- Lambda expressions
- Type inference for local variables
- Generics
- Non-null types
- Assisted memory management
- Exception handling
Keywords
Flow Control
- if, else
- switch, case, default
- break, continue, return, yield
- for, foreach, in
- do, while
- try, catch, finally, throw
Declarations
- namespace, interface, class, struct, enum, delegate, signal, errordomain
- construct, get, set, value, base
- const, static, var, dynamic, weak, unowned
- virtual, abstract, override
- public, protected, private, extern
- throws, requires, ensures, yields
- out, ref
Resource Control
lock, using
Literals
- true, false
- null
Operators
Assignment: =, |=, &=, ^=, +=, -=, *=, /=, %=, <<=, >>=, ++, --
Comparision: ==, !=, <=, >=, <, >, in, is
Arithmethic: +, -, *, /, %, <<, >>, ^, &, |, ~
Logic: !, ||, &&
Pointer: *, &, ->, delete
Lambda Functions: =>
Other: generic <, new, this, typeof, sizeof, as, owned
Differences
Arrays
Local arrays can be initialized like this:
var floats = new float[] { 1, 2, 3 };or without type inference:
float[] floats = { 1, 2, 3 };In class context currently the only supported syntax for array initialization is this:
class ValaSyntax {
/* public */ const float[] FLOATS = { 1, 2, 3 };
}
Interfaces
The GObject type system allows interfaces to have non-virtual methods. Therefore interface methods have to be marked as "public abstract" in Vala:
interface ValaSyntax {
public abstract void do_it()
}This is different from C# where you write:
interface ValaSyntax {
void do_it()
}
Object Construction
As Vala uses GObject as type system, its object construction mechanisms are quite different from those found in other object oriented languages. There's also an example which shows the properties' initialisation steps.
Prerequisites
The GObject type system allows classes like Gtk.Widget to be prerequisites of interfaces. As automatic inheritance from Gtk.Widget by implementing an interface would be confusing, prerequisites are not automatically inherited in Vala:
public interface Foo : GLib.Object {}
public interface Bar : Foo {}
public class FooBar : GLib.Object, Foo, Bar {}In C# or Java specifying just Bar would have been sufficient:
public class FooBar : Bar {}
Properties
Vala has support for construction-only properties:
string filename {
get { return _filename; }
construct { _filename = value; }
}
Namespaces
Namespaces can contain methods:
namespace Gtk {
void main();
}A namespace can be specified at the class definition:
class Namespace.ClassName {
void main();
}