GObject-style Construction
When creating a new class, prefer GObject-style construction. This type of class construction separates handling arguments from the main logic of your class. It also ensures that information passed in during construction is still available later, and makes your class more likely to be compatible with GLib's functions like property binding.
A simple class written with GObject-style construction looks like this:
1
public class MyClass : Object {
2
public int foo { get; construct; }
3
4
public MyClass (int foo) {
5
Object (foo: foo);
6
}
7
8
construct {
9
if (foo > 0) {
10
// Do stuff
11
} else {
12
// Do other stuff
13
}
14
}
15
}
Copied!

Construction Properties

1
public int foo { get; construct; }
Copied!
In the above example, MyClass has a public property foo whose type is int, but we've also declared it as { get; construct; }. This shorthand declares the type of access for this property's get and set functions. Since we declared the property as public, get is also public, but we've declared the set function as construct, which means we can only assign its value as a constructor argument.
We want construction arguments to be public to ensure that we can later get information out of a class that was used to construct it, if need be. But it's important to declare limited set access on properties for future maintainability. Since there is no handling for changes in this property in the construct block, setting this property after the class is constructed would have no effect, even if we allowed it by declaring { get; construct set; }.

Constructors

1
public MyClass (int foo) {
2
Object (foo: foo);
3
}
Copied!
MyClass also contains a constructor; it describes what arguments are required to construct the class. We've declared here that in order to construct MyClass, we need an int passed in when we initialize a new object such as with var new_class = new MyClass (5);
Inside the constructor, we have a special Object () call, in which we specify a property and its value, Object (foo: foo);. This sets the value of the integer property foo defined earlier to the value received as an argument. It is equivalent to saying this.foo = foo;.

The Construct Block

1
construct {
2
if (foo) {
3
// Do stuff
4
} else {
5
// Do other stuff
6
}
7
}
Copied!
When using GObject-style construction, a constructor should only contain code that parses arguments and sets property values with the Object () call. All other class contruction logic happens in the construct block. Code in the construct block runs every time an instance of this object is created, regardless of the constructor used.

Declaring Multiple Constructors

1
public class Row : Object {
2
public string name { get; construct; }
3
public Icon icon { get; construct; }
4
5
public Row (string name, Icon icon) {
6
Object (
7
name: name,
8
icon: icon
9
);
10
}
11
12
public Row.from_device (Device device) {
13
Object (
14
name: device.name,
15
icon: device.icon
16
);
17
}
18
19
construct {
20
var icon = new Gtk.Image.from_gicon (icon, Gtk.IconSize.DND);
21
var label = new Gtk.Label (name);
22
}
23
}
Copied!
To see how this style of class construction scales and keeps code organized, let's look at a more complex example. Say we want to display a list of devices. Each device lives in its own row, which is denoted by the class Row. In this example, sometimes we have access to a Device object which stores the name and icon properties of the device, but for some devices we have to pass in those properties manually to construct our row. We can acheive this by declaring two constructors:
  • a default constructor that takes two arguments: string name and Icon icon
  • a separate, named constructor which takes one argument: a Device object
1
public Row (string name, Icon icon) {
2
Object (
3
name: name,
4
icon: icon
5
);
6
}
7
8
public Row.from_device (Device device) {
9
Object (
10
name: device.name,
11
icon: device.icon
12
);
13
}
Copied!
Note that in both cases, we handle the arguments in the constructor, while the actual logic of creating UI widgets lives in the common construct block.
1
construct {
2
var icon = new Gtk.Image.from_gicon (icon, Gtk.IconSize.DND);
3
var label = new Gtk.Label (name);
4
}
Copied!
This way, we can have multiple constructors without having to repeat the initialization logic.

See also

Last modified 15d ago