benmerckx / genes

Generates split ES6 modules and Typescript definitions from Haxe modules.
44 stars 8 forks source link

Bypass getter inside getter #19

Closed kevinresol closed 4 years ago

kevinresol commented 4 years ago
class Main {
    static var inst(get, null):String;

    public static function main() {
        trace(inst);
    }

    static function get_inst() {
        if(inst == null) inst = 'foo';
        return inst;
    }
}

genes generated:

static get inst() {
    return this.get_inst()
}
static get_inst() {
    if (Main.inst == null) {
        Main.inst = "foo";
    };
    return Main.inst;
}

which overflows because of the mutual recursion

kevinresol commented 4 years ago

Perhaps genes should generate a real underlying variable, e.g.:

static get inst() {
    return this.get_inst()
}
static get_inst() {
    if (Main.__genes__inst == null) {
        Main.__genes__inst = "foo";
    };
    return Main.__genes__inst;
}
benmerckx commented 4 years ago

Oh, that's interesting. I thought this case was only possible with @:isVar, where the native getter would not be generated. Maybe it's different for static properties, I'll have a look.

kevinresol commented 4 years ago

iirc @:isVar is only useful for (get/never, set/never) because null accessor means the var can be accessed within the class, so there would be a physical var.

benmerckx commented 4 years ago

I only generate the native setters/getters for improved interop with js. But as far as generation of code goes I'd like to stay close to what haxe does as maybe some day I can rip out the ExprEmitter for JSGenApi expr/value (which for me depends on haxefoundation/haxe#8625)