The quote at the bottom of the post is from the official lua reference manual at lua.org
I believe that it is possible, through the use of meta tables, to define shorthand syntax for referring to table values. I am interested in this because I have begun to declare by functions inside tables rather than the global space, and also to use larger and larger tables for other values. This has a lot of advantages, but also the disadvantage of requiring extra typing while coding, and also less readability.
The string library provides all its functions inside the table string. It also sets a metatable for strings where the __index field points to the string table. Therefore, you can use the string functions in object-oriented style. For instance, string.byte(s, i) can be written as s:byte(i).
I like that. I want to write functions that have similarly 'nice' shorthand alternative syntaxes.
For example:
function av.prompt.set(t, v)
--the script
end
Can I set it up so that I can call the above function with:
hp:set(4353)
?
The lua reference manual is good, but I can't figure out how to use metatables from the examples it gives.
"index": The indexing access table[key].
function gettable_event (table, key)
local h
if type(table) == "table" then
local v = rawget(table, key)
if v ~= nil then return v end
h = metatable(table).__index
if h == nil then return nil end
else
h = metatable(table).__index
if h == nil then
error(···)
end
end
if type(h) == "function" then
return (h(table, key)) -- call the handler
else return h[key] -- or repeat operation on it
end
end
* "newindex": The indexing assignment table[key] = value.
function settable_event (table, key, value)
local h
if type(table) == "table" then
local v = rawget(table, key)
if v ~= nil then rawset(table, key, value); return end
h = metatable(table).__newindex
if h == nil then rawset(table, key, value); return end
else
h = metatable(table).__newindex
if h == nil then
error(···)
end
end
if type(h) == "function" then
h(table, key,value) -- call the handler
else h[key] = value -- or repeat operation on it
end
end
* "call": called when Lua calls a value.
function function_event (func, ...)
if type(func) == "function" then
return func(...) -- primitive call
else
local h = metatable(func).__call
if h then
return h(func, ...)
else
error(···)
end
end
end