Vala FAQ


Why have you created a whole new programming language? Why didn't you just use C++, C#, D, Java, Python,...?

The type system of GObject doesn't fit perfectly well with the type system of any existing programming language. Vala is designed for GObject, this makes it easy to develop GObject-based libraries with Vala that can be used from a variety of other languages and runtime environments just like all the other GObject-based libraries out there. Just like Qt's MetaObject is an extension to C++ to provide signals, slots, and object properties, Vala is a modification of C# to better match the GObject type system. We want to use the same syntax as C# wherever it makes sense to keep the entry barrier low.

How can I use Vala libraries from C, C++, C#, D, Java, Python,...?

You can always use Vala libraries just like they were GObject/C libraries, they provide C header files and use GObject for all classes. Work is in progress to be able to generate bindings to other languages very easily from the Vala source files, that should eliminate most, if not all, work to create bindings for Vala libraries.

How does the performance of Vala applications compare to other applications?

The performance should be pretty similar to GObject/C-based code as there is no Vala-specific runtime library/environment that needs to be loaded. The C compiler can also apply the same optimizations on Vala-generated C code and plain GObject/C code. The Vala compiler uses reference counting in more places than most GObject/C-based applications do. However, Vala allows to fine-tune that easily in performance-critical sections with the weak modifier.

Vala is quite similiar to C#. Do you plan to replace Mono?

Vala wants to be a convenient tool for creating libraries which can be consumed by any programming language supported by the GNOME platform - including Mono. Vala tries to integrate, not to separate.

What does "string? foo" mean?

The ?-modifier tells the vala compiler that a passed or returned value may be null.

Can I use keywords as identifiers?

Yes, if you prepend the identifier with an '@'.

Why do I have to inherit from GObject?

Since Vala is based on GObject it's needed to inherit almost every class from GObject. However, it's not mandatory, but if you don't inherit, you will only get a very restricted class. Unless you know what you're doing you want to inherit from GObject. The ../BasicSample shows how to achieve a full-featured class.

Can I use pointer arithmetic inside strings?

One reason why it's not allowed is that Vala strings are encoded in UTF-8, which uses a variable number of bytes per character. This means that replacing a single character as in your example might require resizing the string, which we don't want to happen implicitly.

There are two possibilities how to modify strings in Vala. The recommended way is to use the GLib.StringBuilder class, which is a binding for GString. The other possibility is to use raw pointers just like in C, however, you have to care about encoding and memory management yourself, then.

char* str_ptr = new char[64];
str_ptr[0] = 'e';
string str = (string) str_ptr;
delete str_ptr;

(source)

How to test if an object is some class or subclass?

Use the "is" operator.

var wi = Gtk.Button();
if (wi is Gtk.Widget)
    stdout.printf("Is a Widget.\n");
if (wi is Gtk.Button)
    stdout.printf("Is a Button.\n");

How to force cast an object from one type to another type?

Use "(Klass) object".

Gtk.Button btn1 = (Gtk.Button) awidget;

Does Vala have a preprocessor?

Starting with version 0.7.0, Vala supports the preprocessing directives #if, #elif, #else, and #endif. The supported operators for conditionals are ==, !, && and ||. There is no intention to ever support macros as found in the C preprocessor.

int main (string[] args) {
#if COND
  message ("COND IS DEFINED");
#else
  message ("COND IS NOT DEFINED");
#endif
  return 0;
}

You must add -D COND to the valac command line to enable conditional compilation for the above example.

What does [SimpleType] and [Compact] in bindings mean?

Have a look at this graphic:

vala-structs-classes.png

SVG version: vala-structs-classes.svg

How do I read from stdin?

Allocating space for strings to be read in can be tricky. See Vala/InputSamples for an example.

Vala/FAQ (last edited 2009-04-07 21:13:12 by IainF)