meta-method

How do we change the way print displays a table

南楼画角 提交于 2019-12-10 22:07:10
问题 Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2 . So far I've tried setting the __tostring metamethod but that doesn't seem to be invoked by print . Is there some metamethod I've been missing or does the answer have nothing to do with metamethods? 回答1: __tostring works: aTable = {aValue=1} local mt = {__tostring = function(t) local

Lua override # for strings

旧街凉风 提交于 2019-12-10 20:25:32
问题 I'm trying to imlement my own length method for strings in Lua. I have successfully overriden len() method for string, but I have no idea how to do this for # operator. orig_len = string.len function my_len(s) print(s) return orig_len(s) end string.len = my_len abc = 'abc' If I call: print(abc:len()) It outputs: abc 3 But print(#abc) Outputs only '3' and that means it called original length function instead of mine. Is there a way to make # call my length function? 回答1: I'm trying to imlement

what is actual implementation of lua __pairs?

心不动则不痛 提交于 2019-12-07 02:30:38
问题 Does anybody know actual implementation of lua 5.2. metamethod __pairs ? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs() ? I need to override __pairs and want to skip some dummy variables that I add in a table. 回答1: The following would use the metatable meta to explicitly provide pairs default behavior: function meta.__pairs(t) return next, t, nil end Now, for skipping specific elements, we must replace the returned next :

what is actual implementation of lua __pairs?

岁酱吖の 提交于 2019-12-05 06:06:49
Does anybody know actual implementation of lua 5.2. metamethod __pairs ? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs() ? I need to override __pairs and want to skip some dummy variables that I add in a table. The following would use the metatable meta to explicitly provide pairs default behavior: function meta.__pairs(t) return next, t, nil end Now, for skipping specific elements, we must replace the returned next : function meta.__pairs(t) return function(t, k) local v repeat k, v = next(t, k) until k == nil or theseok(t,

Confusion of using “.” notation with __index and namespace in Lua

谁说我不能喝 提交于 2019-11-29 15:50:08
I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index ? list = {} list.__index = list setmetatable(list, { __call = function(_, ...) local t = setmetatable({length = 0}, list) for _, v in ipairs{...} do t:push(v) end return t end }) function list:push(t) if self.last then self.last._next = t t._prev = self.last self.last = t else self.first = t self.last = t end self.length = self.length + 1 end . . . local l =

Confusion of using “.” notation with __index and namespace in Lua

落爺英雄遲暮 提交于 2019-11-28 09:24:04
问题 I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index ? list = {} list.__index = list setmetatable(list, { __call = function(_, ...) local t = setmetatable({length = 0}, list) for _, v in ipairs{...} do t:push(v) end return t end }) function list:push(t) if self.last then self.last._next = t t._prev = self