Re: [Vala] Base class constructor
- From: Jürg Billeter <j bitron ch>
- To: Ondřej Jirman <megous megous com>
- Cc: vala paldo org
- Subject: Re: [Vala] Base class constructor
- Date: Fri, 25 Aug 2006 13:13:41 +0200
Hi Ondrej,
On Don, 2006-08-24 at 23:23 +0200, Ondřej Jirman wrote:
I've been playing with vala quite a bit and found a problem. I ported
simple gtk application to vala just to see how it will work.
I can't figure out how to call base class constructor. I've looked at
grammar spec and it hase base keyword. This is necessary so that if I
derive class from some exiting Gtk class (say Gtk.Window)
it gets properly initialized.
Object construction in GObject works quite differently compared to
popular object-oriented languages like C++, Java, or C#. Whereas in C#
you have just one or multiple overloaded constructors per class which
invoke one of the constructors of the base class, there are quite a few
more components used for construction in GObject and therefore Vala.
The first thing you do is calling a construction method, i.e. foo_new()
in C or new Foo() in Vala. You can declare construction methods as
follows:
public construct [identifier] ([parameter-list]) { body }
If you specify the optional identifier, you invoke it with
foo_new_identifier() resp. new Foo.identifier()
Construction methods call g_object_new with an optional list of property
name/value pairs to initiate the construction process. g_object_new
allocates memory and initializes by calling the instance_init methods of
all base classes and finally the class itself. If you declare a field in
Vala with an initializer then it will be executed now. After instance
initialization g_object_new sets all construction properties, i.e. the
body of all construction properties will be executed now. You can
declare construction properties in Vala as follows:
public type-name identifier {
get { body }
[set] construct { body }
}
After having set the construction properties, the overridden
constructors of all base classes and the class itself will be called,
starting with the constructor of GObject. That guarantees that all base
class constructors get invoked so that the object is properly
initialized. There may only be one constructor per class in Vala and it
doesn't take any arguments.
public class-name () { body }
That's the place to put code that should get executed for every instance
created. When the constructors have run g_object_new sets all
non-construction properties that have been specified in the construction
method. g_object_new returns afterwards and the remaining code in the
construction method gets executed, often nothing or just a method call
remaining, there shouldn't be much code logic in construction methods.
I hope that helps in understanding the process of constructing objects
in Vala. If you have further questions I recommend you to read the
section about GObject instantiation[1] in the GObject API reference and
ask here for more details.
The reason your simple gtk application doesn't work is that you have to
initialize GTK+ before creating the window, i.e. add the following line
to the top of your main function:
Gtk.init(out args);
BTW: The `base' keyword will be used to chain up in virtual methods, it
hasn't been implemented yet, though.
Regards,
Jürg
[1] http://developer.gnome.org/doc/API/2.0/gobject/chapter-gobject.html#gobject-instanciation
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]