instanceof and Type Introspection
Implement instanceof() checks by walking the class hierarchy.
instanceof and Type Introspection is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is instanceof?
instanceof(obj, Class) checks whether an object is an instance of a specific class or any of its ancestors. It replaces Lua's raw type() for OOP systems.
Simple instanceof
Walk the metatable chain from the instance upward, looking for the target class.
local function instanceof(obj, cls)
if type(obj) ~= "table" then return false end
local mt = getmetatable(obj)
while mt do
if mt == cls then return true end
local parent = getmetatable(mt)
mt = parent and parent.__index
end
return false
endTesting instanceof
Works correctly across inheritance chains.
local cat = Animal:new("Cat","Meow")
local rex = Dog:new("Rex")
print(instanceof(rex, Dog)) -- true
print(instanceof(rex, Animal)) -- true
print(instanceof(cat, Dog)) -- false__type Field Convention
Store a string type name in each class for fast runtime type checks.
Animal.__type = "Animal"
Dog.__type = "Dog"
local function typeOf(obj)
local mt = getmetatable(obj)
return mt and mt.__type or type(obj)
end
print(typeOf(rex)) -- DogType Guards
Create type guard functions for cleaner assertion at API boundaries.
local function assertType(obj, cls, argName)
if not instanceof(obj, cls) then
error(("Expected %s for %s, got %s"):format(
cls.__name, argName, typeOf(obj)), 2)
end
endgetmetatable for Class Detection
getmetatable(obj) returns the class table. Compare with a known class to test exact type (not inheritance-aware).
local function exactType(obj)
return getmetatable(obj)
end
print(exactType(rex) == Dog) -- true
print(exactType(rex) == Animal) -- false (not exact)Class Hierarchy Introspection
Walk the metatable chain to list all classes an object inherits from.
local function classChain(obj)
local chain = {}
local mt = getmetatable(obj)
while mt do
chain[#chain+1] = mt.__name or "?"
local parent = getmetatable(mt)
mt = parent and parent.__index
end
return chain
endComparing Type Systems
Lua's built-in type() returns only 8 basic types. For OOP systems, use instanceof or custom typeOf functions that understand the class hierarchy.
Sealed Classes
Prevent subclassing by removing the :extend() method from a class or adding a check in __index.
Type-Based Dispatch
Dispatch function calls based on runtime type — a lightweight alternative to method overloading.
local function process(animal)
if instanceof(animal, Dog) then
print("Processing Dog:", animal.name)
elseif instanceof(animal, Animal) then
print("Processing Animal:", animal.name)
end
endDebugging with Type Info
Add __tostring to classes that includes the type name for meaningful debug output.
instanceof Question
What does instanceof(rex, Animal) return if Rex is a Dog (which extends Animal)?
Recap: instanceof and Type Introspection
instanceof walks the metatable chain for inheritance-aware type checking. Use __type fields for fast string type lookup. Type guards at API boundaries catch type errors early with clear messages.
Frequently asked questions
Is the “instanceof and Type Introspection” lesson free?
Yes — the full text of “instanceof and Type Introspection” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “instanceof and Type Introspection”?
Implement instanceof() checks by walking the class hierarchy. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “instanceof and Type Introspection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Base Class and Constructor Design
- Multi-Level Inheritance
- Mixin Composition and Interface Checks
- instanceof and Type Introspection