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)
This commit is contained in:
1 parent
4dae4ee1cd
commit
1203acd64b
8 files changed
+381
-14
No files matched your search
@@ -0,0 +1,23 @@
|
||||
-- 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)
|
||||
Reference in new issue
Block a user