GLib.Object.new problem

Hello,
I have just discovered a strange bug in Vala 0.44.3

Consider the code bellow:

using GLib;
public class ABC : GLib.Object {
    public static int main (string[] args) {
    Object a = new Object (typeof (ABC));
    assert (a.get_type () == typeof (ABC));
    return 0;
  }
}

The code looks valid and compiles, but it does not run.

You will get the following error:

(process:9209): GLib-GObject-CRITICAL **: 23:15:21.563: g_object_new_is_valid_property: object class 'GObject' has no property named '\u0001'

So I did some digging and found that the code gen is doing something very wrong, as this is the generated C code:

gint
abc_main (gchar** args,
          gint args_length1)
{
	gint result = 0;
	GObject* a = NULL;
	GObject* _tmp0_;
#line 6 "../test.vala"
	_tmp0_ = g_object_new (G_TYPE_OBJECT, TYPE_ABC, NULL);
#line 6 "../test.vala"
	a = _tmp0_;
#line 8 "../test.vala"
	_vala_assert (G_TYPE_FROM_INSTANCE (a) == TYPE_ABC, "a.get_type () == typeof (ABC)");
#line 10 "../test.vala"
	result = 0;
#line 10 "../test.vala"
	_g_object_unref0 (a);
#line 10 "../test.vala"
	return result;
#line 67 "test.c"
}

As you can see the g_object_new (...) is incorrect.

Or is this the new behaviour for this? How do you set construct-only properties if you can’t do it this way?

Regards
Gustav Hartvigsson

The binding for g_object_new is Object.new(), not new Object(). Here is an example of a construct-only property using both Object.new() and a normal constructor:

public class ABC : GLib.Object {

    public string prop { get; construct; }

    public ABC (string prop) {
        Object (prop: prop);
    }

}

int main (string[] args) {
    var a = Object.new (typeof (ABC), "prop", "foo");

    var b = new ABC("foo");

    return 0;
}

I’ll be damned!
I mean, I’ve been using Vala now-and-then for years, I should know this, yet something as simple as this stumped me for hours.

Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.