Re: [Vala] Base class constructor
- From: Ondřej Jirman <megous megous com>
- To: Jürg Billeter <j bitron ch>
- Subject: Re: [Vala] Base class constructor
- Date: Sat, 26 Aug 2006 00:26:24 +0200
Thank you for your reply. It helped. Distinction between construction
methods and constructors was nonobvious to me.
Here is sample code that demonstrates construction of objects in vala:
(in case someone else will have same problem as I did)
----------------------------------------------
using GLib;
class Test.Base
{
public construct()
{
stdout.printf("base construction method\n");
}
public Base()
{
stdout.printf("base constructor\n");
}
}
class Test.Ext : Test.Base
{
public Ext()
{
stdout.printf("ext constructor\n");
}
public construct()
{
stdout.printf("ext construction method\n");
}
}
namespace Test
{
static int main (string[] args)
{
stdout.printf("new Ext()\n");
var o1 = new Ext();
stdout.printf("new Base()\n");
var o2 = new Base();
return 0;
}
}
It outputs:
new Ext()
base constructor
ext constructor
ext construction method
new Base()
base constructor
base construction method
-------------------------------------------------
Now I'm able to derive my own classes from gtk+ classes. Here is simple
hello world example if anyone is interested:
using Gtk;
using GLib;
namespace Hello
{
public class MainWindow : Gtk.Window
{
private Button close_button;
private void on_hide()
{
Gtk.main_quit();
}
private void on_close_clicked(Button b)
{
hide();
}
public construct()
{
close_button = new Button();
close_button.label = "Hello World!";
add(close_button);
show_all();
hide += on_hide;
close_button.clicked += on_close_clicked;
}
}
static int main (string[] args)
{
Gtk.init(out args);
var main_win = new MainWindow();
Gtk.main();
return 0;
}
}
-------------------------------------------------
I will try to port my package selector for slackware that is written in
C++ (gtkmm) to vala and give some feedback if I found something
interesting that may be of any use to vala (bugs, feelings, etc.).
I have question about use of lamda functions to handle signals thrown by
GTK+ objects. I can't get it to work. I don't know what I'm doing wrong.
I've tried to use it the same way as it is used in test-18.vala.
Instead of 'close_button.clicked += on_close_clicked' in the hello world
example I use:
close_button.clicked += (close_button) => {
hide();
};
And valac fails to compile it with error:
pkgsetup.vala:58.46-58.47: error: syntax error, unexpected =>,
expecting ;
pkgsetup.vala:60.8-60.8: error: syntax error, unexpected ;
pkgsetup.vala:71.1-71.1: error: syntax error, unexpected }
Compilation failed: 3 error(s), 0 warning(s)
Is this just not yet implemented or am I doing something wrong again?
BTW, how is determined type of arguments passed to the lambda function?
Is it from the type of arguments I am passing to it or is it from the
context I am using it in? Like callback function type or signal
declaration.
Regards,
Ondrej
On Fri, 2006-08-25 at 13:13 +0200, Jürg Billeter wrote:
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]