javascriptgnomegobjectgjs

GObject.ParamFlags.CONSTRUCT_ONLY throws error unless it's also WRITABLE


im trying to learn gjs and having a few questions which i think are basic about ParamFlags. I'm creating a subclass and trying to use GObject.ParamFlags.CONSTRUCT_ONLY | GObject.ParamFlags.READABLE for one of the parameters. The use case being what it sounds like, being able to set it only during construction and later being able to read it. I'm declaring it like:

magic_word: GObject.ParamSpec.string(
        "magic-word",
        "Magic word",
        "My magic word",
        GObject.ParamFlags.CONSTRUCT_ONLY | GObject.ParamFlags.READABLE,
        "magicword default value"
      ),

But this throws the following error during module loading.

GLib-GObject-CRITICAL **: 14:18:53.750: validate_pspec_to_install: assertion 'pspec->flags & G_PARAM_WRITABLE' failed

It still allows the program to continue execution though.

It seems the only way to get this error to stop is to also give it the WRITABLE or READWRITE flag and perhaps implement the desired behavior through getter/setter. But then the purpose of CONSTRUCT_ONLY seems lost. What am I missing?


Solution

  • G_PARAM_CONSTRUCT_ONLY means a property can only be set during construction, implying that the property must be writable. In other words, CONSTRUCT_ONLY is simply a constraint on WRITABLE.

    The merge request adding JS-level support for G_PARAM_CONSTRUCT_ONLY might help clarify how this works in GJS: https://gitlab.gnome.org/GNOME/gjs/-/merge_requests/377. Keep in mind this was only merged in November 2020.