Skip to content

overrides: Allow users to implement construct-only props with getters

Evan Welsh requested to merge ewlsh/construct-only-props into master

Currently construct-only props cannot be defined by the class implementing them, they can only be passed to the parent constructor. This behavior is unexpected and unintuitive when implementing custom GObject classes with standard JS class constructors and fields.

Current

GObject.registerClass({
    Properties: {
        'construct': GObject.ParamSpec.int('construct', 'ParamConstructOnly',
            'A readwrite construct-only parameter',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
            ''),
}, class MyObject extends GObject.Object {
    _construct = 5;

    constructor({ construct }) {
        // Currently you have to do this to define 'construct'
        super({ construct });

        // This is pointless
        this._construct = construct;
    }

    get construct() {
        // Never called
        return this._construct;
    }

    set construct() {
        // Never called
        this._construct = 100000000000;
    }
});

Proposed

GObject.registerClass({
    Properties: {
        'construct': GObject.ParamSpec.int('construct', 'ParamConstructOnly',
            'A readwrite construct-only parameter',
            GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
            ''),
}, class MyObject extends GObject.Object {
    _construct = 5;

    constructor({ construct }) {
        super();

        // This is required
        this._construct = construct;
    }

    get construct() {
        // Called
        return this._construct;
    }

    set construct() {
        // Never called
        this._construct = 100000000000;
    }
});

Depends on !700 (merged)

Edited by Philip Chimento

Merge request reports