You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was playing around with trying to find a class style pattern I like with the new interface features, so I tried this:
local interface State
update: function(self: State, dt: number)
-- a metatable that you can use to get default implementations
record mt end
end
State.mt = {
__index = {
update = function(self: State, dt: number)
print("default update")
end
}
}
-- Now make a State.
local record GameState is State end
setmetatable(GameState, State.mt)
local gameState_mt: metatable<GameState> = {__index = GameState}
function GameState.new(): GameState
return setmetatable({}, gameState_mt)
end
function GameState:foo()
print("foo")
end
local game = GameState.new()
game:foo()
game:update(1)
This fails because State is elided from the generate code:
State.mt = {
__index = {
update = function(self, dt)
print("default update")
end,
},
}
local GameState = {mt = {}, }
setmetatable(GameState, State.mt)
local gameState_mt = { __index = GameState }
function GameState.new()
return setmetatable({}, gameState_mt)
end
function GameState:foo()
print("foo")
end
local game = GameState.new()
game:foo()
game:update(1)
A very similar pattern using records would work (basically just make State a record and remove the is).
So either:
This is maybe philosophically against how interfaces are supposed to work and it's just a gap in validation that nested records are allowed.
Or maybe when nested records are detected in interfaces, the interface needs to generate a container in the Lua code.
The text was updated successfully, but these errors were encountered:
I think that's a reasonable take (option 1 above). I see other issues in this repo where people tried to nest a record in an interface. A simpler reproduction:
local interface Foo
record Baz
x: number
end
end
function Foo.Baz.new(x: number): Foo.Baz
return {x = x}
end
It seems that the compiler should prevent nesting records in interfaces.
I was playing around with trying to find a class style pattern I like with the new interface features, so I tried this:
This fails because
State
is elided from the generate code:A very similar pattern using records would work (basically just make State a record and remove the
is
).So either:
The text was updated successfully, but these errors were encountered: