0

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?

keponk
  • 281
  • 1
  • 3
  • 14

1 Answers1

1

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.

andy.holmes
  • 3,383
  • 17
  • 28
  • ah! this makes sense. So in the future it would behave as documentation "suggests". In general it seems there's a lot of small "gotchas" like this in the process of learning gtk/gjs at the same time without prior knowledge of either. In any case, this and the thread answered my question. thanks! – keponk Dec 27 '20 at 01:09
  • Right, in future it should work as advertised. Probably this wasn't a priority since GJS is rarely used to write public APIs and is usually just a consumer of C APIs, but you can always enforce the rules yourself if you need to. – andy.holmes Dec 27 '20 at 01:37