function Class:GetList(something: {[number]: T}): {[number]: T}
But I don’t know where to put the <T>.
It doesn’t work after function, doesn’t work before or after Class, doesn’t work before or after GetList and doesn’t work after the parenthesis or after the return type either.
How do I tell Luau that this function is generic? I feel like this has the same issues as trying to assign to a field in self, that is, you have to do something like this:
local something: Something = {}
self.something = something
But I feel like it would look even weirder to do that with methods…
According to Type checking | Luau, there doesn’t seem to be a way. You could feasibly do this at runtime by calling typeof or checking metatables, which seems to be the next best thing.
In this case, if poll is present, it will be called and the result moved into a copy of tbl.
Can the type checker infer this correctly?
function Class:GetList(tbl: {[number]: any}, poll)
local built: typeof(tbl) = {}
table.move(tbl, 0, #tbl, 0, built)
if poll then
local more = poll()
table.move(more, 0, #more, #built, built)
end
return built
end