Only allow calls to public methods and setting properties in custom constructors of GObject derived classes
Often people encounter the problem, that a property for example isn't set or code didn't run when constructing an object for example through a GTK UI file, because only the construct {}
block, and not the custom constructor is run.
To make people more aware of that custom constructors are really just a convenience wrapper around the Object()
call, there could be a few rules set, what is allowed in a custom constructor, and when these rules are violated a warning for example appears.
The rules would restrict the access to the object that is currently constructed (this
):
- Only set properties through the
Object()
call - Only call public methods on the object
Everything else would still be allowed. Here is an example:
class MyClass : Object {
public MyClass (int i, float f, string s) {
int j = i + 100;
var w = new Gtk.Window ();
//wrong and not allowed:
this.i = j;
this.w = w;
//correct
Object (i: j, w: w);
//wrong:
this.s = s + "\n";
//correct
this.do(s);
}
//correct
public void do (string s) {
this.s = s + "\n";
}
}