- CAT_TYPES_FUNCTION_Closure gains an attrs table; get/set_attribute give closures real members, so a function can hold _doc_ and any other attribute like a table - cat_types_function_attrs() accessor; attributes() enumerates a closure's members; help() reads a function's _doc_ - compiler: generalise dotted assignment to chains (name.a.b = v), so a method can be documented (Calc.add._doc_ = ...) - tests/functions_as_objects.cat + 7 assertions (84 total, all green) - README: 'funcoes tambem sao objetos' + reflection sections
29 lines
828 B
Plaintext
29 lines
828 B
Plaintext
-- tests/functions_as_objects.cat -- functions are objects too.
|
|
--
|
|
-- A closure carries a member table, so it can hold `_doc_` (and any
|
|
-- other attribute) exactly like a table. `help()` reads it; a dotted
|
|
-- assignment can reach an attribute of a nested object (a method).
|
|
|
|
local t = {x = 1, y = 2}
|
|
t._doc_ = "A point."
|
|
|
|
function add(a, b) return a + b end
|
|
add._doc_ = "add(a, b) -- soma dois numeros."
|
|
print("call:", add(2, 3))
|
|
print("name:", add.name)
|
|
print("doc:", add._doc_)
|
|
|
|
for a in attributes(add) do print("attr:", a) end
|
|
|
|
-- documenting a method: nested (chained) attribute assignment
|
|
class Calc
|
|
function add(self, a, b) return a + b end
|
|
end
|
|
Calc.add._doc_ = "add(a, b) on Calc."
|
|
print("method doc:", Calc.add._doc_)
|
|
local c = Calc()
|
|
print("method call:", c:add(4, 5))
|
|
|
|
-- help() reads a function's _doc_
|
|
help(add)
|