Problems with inherited getters and setters

I have an abstract class with a protected variable, and a getter and setter for it. I know that arcade doesn’t allow abstract functions, so I gave them default implementations.

abstract class Foo {
    protected _x: number;
    public get x(): number {
        return this._x;
    }
    public set x(nx: number) {
        this._x = nx;
    }
    constructor() {
        this._x = 0;
    }
}

I have a subclass that I want to treat the member as readonly, like this:

class Bar extends Foo {
    // getter required to match setter, but left unaltered.
    public get x(): number {
        return this._x;
    }
    // no setting allowed, functionally making _x readonly
    public set x(nx: number) {
        throw "nuh uh";
    }
    constructor(x: number) {
        super()
        this._x = x
    }
}

Tested using this:

let testBar = new Bar(5);
game.splash("bar x: ", "" + testBar.x);

It threw the error in the constructor, despite both the base constructor and the overridden one not using the setter. When I replaced the throw with a game.splash("no"), the test output was “bar x: undefined”. Also interestingly, when I followed up the test code with testBar.x = 3 the slash “no” appeared two more times. Also tried making the class not abstract, but that had no effect. Tried doing let testBar: Foo = new Bar(5), but that had no effect.

Not sure what exactly is doing on under the hood, or how to fix it. I need the variable to be settable in the superclass but not in the subclass. I tried making it public in the superclass and private in the subclass, but that leads to “redefinition of _x as a field”. I know that the override keyword isn’t usable, and I can’t make the getter and setter abstract. The Omit, Exclude and Pick keywords don’t seem to work either.

Please let me know if I’m approaching this in the wrong way. Thanks!

1 Like

I’m not sure what has changed but the issue seems to be fixed. The test code correctly throws the error, and changing it to a splash screen works as well. I’m not sure about Omit, Exclude, and Pick but the code works fine. Maybe it was an update to makecode?