Search code examples
luaintrospection

What are Lua's Introspection features?


What are Lua's introspection features? I know that you can query the type of a variable at runtime using type(var) and that the debug package provides some features for inspecting the environment, but it is not clear what that gives me.

What other introspection features are in Lua? Any good resources?


Solution

  • Lua values can have 7 types: nil, boolean, number, string, function, userdata, thread, and table. You can get the type of a value using the type function from the standard library.

    If you are working with tables, you can iterate over its keys using the pairs function.

    Finally, values in Lua can have metatables and this is often used in to program in an object oriented style. You can get a value's metatable using the getmetatable function.