Files
Minus 1203acd64b reflection: attributes() (dir-like) and help() with _doc_
- new list type (iterable, indexable) backing attributes()
- attributes(obj): member names of a table/object, follows __index
  inheritance, includes native and slot members (like Python dir())
- help(obj): prints the value's _doc_ documentation, else its type
  and attributes
- natives carry an optional _doc_ string (cat_types_native_new_doc);
  stdlib builtins documented
- len() handles lists
- examples/reflect.cat + 8 assertions wired into run_tests.sh (77 total)
2026-11-16 03:48:44 +00:00

24 lines
707 B
Plaintext

-- examples/reflect.cat -- reflection: attributes() and help().
--
-- `attributes(obj)` lists an object's members (like Python's dir()),
-- following inheritance via `__index`. `help(obj)` prints an object's
-- documentation, read from its `_doc_` attribute.
local point = {x = 0, y = 0, name = "origin"}
point._doc_ = "A 2D point with x, y coordinates."
print("point attrs:", len(attributes(point)))
for a in attributes(point) do print(" -", a) end
class Animal
function speak(self) print("...") end
function name(self) return "animal" end
end
local a = Animal()
print("animal has speak:", len(attributes(a)) > 0)
print("--- help(point) ---")
help(point)
print("--- help(print) ---")
help(print)