[Vala] Confused with constructors



The following code causes c compilation to fail.

public class Baz : Object {

        public string baz_string { get; construct; }

        public Baz (string arg) {
                if (arg.length > 2) this.baz_string = arg;
                else this.baz_string = "done";
        }
}

COMPILE:
construct.c: In function 'baz_construct':
construct.c:34: error: '__params_it' undeclared (first use in this function)


It appears that the constructoresque Baz () may be limited to setting
properties only in the form (this.baz_string = arg). Next question...

public class Bar : Object {
        public string bar_string { get; construct; default = "not set";}
        public int bar_int = 42;

        public Bar () {
                message ("In Bar ()");
                bar_int = 12;
                bar_string = "hello";
        }
        construct {
                message ("In Bar's constructor with bar_string = %s and bar_int =
%d.", this.bar_string, this.bar_int);
        }
}
public void main () {
        var obj = new Bar ();   
}

OUTPUT:
** Message: construct.vala:32: In Bar's constructor with bar_string =
hello and bar_int = 42.
** Message: construct.vala:27: In Bar ()


Additional lines in Bar () are executed AFTER the property is set to
"hello" and after the construct {} block is called, even though
this.bar_string = "hello" is the last line in Bar (). How does vala
choose which code in bar to execute first? Should vala be made to
abort with an error if it sees something besides setting a property
in the constructoresque property setting method?

If Bar () sets member variables instead of properties the members will
be initialized after the construct {} block is called. bar_int retains
the value 42 until after the construct block is called.

Perhaps my confusion stems from a general lack of understanding
concerning properties and GObject. When would I use a property
vs. just a regular instance variable and what is a managed property? I
have used the C api to Gtk and dug into the glib/gobject source but I
never really grasped the reason properties exist. The inability to
compile certain things in the constructoresque property setter plus
its control flow complexity has me scratching my head. The Vala
tutorial and faq are correct in their treatment of these issues but
they are also
very minimal.

Another odd thing:

public class Foo : Object {
        public int foo_int = 42;
        public Foo foo_foo { get; construct; }

        public Foo () {
                foo_foo = this;
        }
        construct {
                message ("%d", foo_foo.foo_int);
        }
}
public void main () {
        var obj = new Foo ();   
}

The above code segfaults. foo_foo is a property so it should be set in
Foo before construct {} is called but apparently it isn't because the
fault occurs in foo_constructor(). Is this a bug? Can properties have
an Object type?

Also, is it (or will it be) possible to overload the constructoresque
property setter on type and arity? I currently get an "already
defined" error.

Finally, what is the constructoresque property setter actually called,
or is "Constructoresque property setter" a good name?

-Sam Danielson



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]