Search code examples
vala

Vala object class lighter than normal class


"What happens if you don't inherit from Object? Nothing terrible. These classes will be slightly more lightweight, however, they will lack some features such as property change notifications, and your objects won't have a common base class. Usually inheriting from Object is what you want." Vala team said.

So I wanted to know how light the classes are with or without inheriting form Object.

So, Here are my test files

test1.vala:

class Aaaa : Object {
    public Aaaa () { print ("hello\n"); }
}
void main () { new Aaaa (); }

test2.vala:

class Aaaa {
    public Aaaa () { print ("hello\n"); }
}
void main () { new Aaaa (); }

The results after the compilation was totally unexpected, the size of test1 is 9.3 kb and the size of test2 is 14.9 kb and that contradicts what they said. Can someone explain this please?


Solution

  • You are comparing the produced object code / executable size, but that's not what the statement from the tutorial was referring to.

    It is refering to the features that your class will support. It's just clarifying that you don't get all the functionality that GLib.Object / GObject provides.

    In C# (and in Java, too?) the type system is "rooted" which means all classes always derive implicitly from System.Object. That is not the case for Vala. Vala classes can be "stand alone" classes which means that these stand alone classes don't have any parent class (not even GLib.Object / GObject).

    The code size is bigger, because the stand alone class doesn't reuse any functionality from GLib.Object / GObject (which is implemented in glib), so the compiler has to output more boiler plate code (writing classes in C is always involving a lot of boiler plate code).

    You can compare yourself with "valac -C yourfile.vala" which will produce a "yourfile.c" file.