Here is an example.
[ UPDATE: I guess this example is not particularly practical or useful. In order to be useful, the record's table (for example: C.D) needs to be set to the __index field of a separate metatable table. However, the example still does expose curious and unexpected behavior by the compiler. ]
global record A end
local record B end
local record C
record D end
end
setmetatable ( {} as A, A )
-- The above line causes a compile time error. But why?
-- argument 2: A is not a metatable<T>
setmetatable ( {} as B, B )
-- The above line causes a compile time error. But why?
-- argument 2: B is not a metatable<T>
setmetatable ( {} as C, C )
-- The above line causes a compile time error. But why?
-- argument 2: C is not a metatable<T>
setmetatable ( {} as C.D, C.D )
-- The above line compiles successfully without any error.
print 'Success.'
$ tl run test.tl
========================================
3 errors:
test.tl:10:25: argument 2: A is not a metatable<T>
test.tl:14:25: argument 2: B is not a metatable<T>
test.tl:18:25: argument 2: C is not a metatable<T>
----------------------------------------
3 errors
Discussed in #1125
Originally posted by parke May 29, 2026
Why are nested records of type
metatable<T>, while top-level records are not?Is this behavior (and the rationale behind it) documented somewhere?
Thanks!
Here is an example.
[ UPDATE: I guess this example is not particularly practical or useful. In order to be useful, the record's table (for example:
C.D) needs to be set to the__indexfield of a separate metatable table. However, the example still does expose curious and unexpected behavior by the compiler. ]Here is what happens when I try to
tl runthe above code.