Lua - Reflection - function parameters and docstrings? -
i'm coming lua python. using lua c api. wonder if there standard method bundle parameter , usage information methods , tie standard help() or <method>.__doc__()-like method.
some ideas had:
1) somehow put docs in library metatable , let users use pairs():
static const lual_reg lua_mylib_funcs[] = { ... {null, null}}; 2) print usage info when methods called no parameters.
3) create .help() or .docs() method library.
can point in "lua-ish" direction?
i wonder if there a standard method bundle parameter , usage information methods
nope.
somehow put docs in library metatable , let users use pairs():
you establish convention if method name foo store docs in foo_docs or that.
x.foo_docs = "returns sum of 3 numbers" function x:foo(a,b,c) return + b + c end print usage info when methods called no parameters.
that prevent creating methods no parameters.
can point in "lua-ish" direction?
kinda hard without know why need , how you'd prefer work. <method>.__doc__ convert method (i.e. function) callable table, let index , store metadata want, ugly , require creating new table per method. instance, let convert method callable table:
local documentmethodmetatable = {} function documentmethodmetatable.__call(t,...) return t.method(...) end function documentmethod(method, doc) return setmetatable({ method=method, doc=doc}, documentmethodmetatable) end then write stuff like:
local foo = {name="donut"} function foo:sum(a,b,c) print(self.name .. " says sum " .. (a + b + c)) end foo.sum = documentmethod(foo.sum, "receives 3 arguments , prints sum.") foo:sum(2,2,3) --> call sum print(foo.sum.doc) --> index sum docs
Comments
Post a Comment