Skip to content

Default constructor on subclass #288

@yancouto

Description

@yancouto

If you do not declare a constructor on a subclass, a default constructor is created and it does not call the superclass one, which creates behavior inconsistent with ts.

Here is the example:

class C {
    x: number
    constructor(y: number) {
        this.x = y
    }
}

class D extends C {
}

const d = new D(12);
// d.x should be 12

This is transpiled to

C = C or {}
C.__index = C
function C.new(construct, ...)
    local self = setmetatable({}, C)
    if construct and C.constructor then C.constructor(self, ...) end
    return self
end
function C.constructor(self,y)
    self.x = y;
end
D = D or C.new()
D.__index = D
D.__base = C
function D.new(construct, ...)
    local self = setmetatable({}, D)
    if construct and D.constructor then D.constructor(self, ...) end
    return self
end
function D.constructor(self)
end
local d = D.new(true,12);
-- d.x is nil

The problem is the default constructor

function D.constructor(self)
end

It should probably be something like this

function D.constructor(self, ...)
    D.__base.constructor(self, ...)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions