我想创建一个 0 内存的 lua 数组,当我在其上使用 #
[]
等运算符时,它实际上会跳转到我的自定义函数
关于如何做到这一点有什么想法吗?
我希望使用这个假数组的用户不会认为它是假的,它在访问速度方面比普通数组差,但具有更好的内存性能
最佳答案
Lua 实现了一种称为元方法的东西 ( documentation )
元方法是存在于表之后的函数,并在某些操作上触发,例如索引数组、读取丢失的索引、收集数组的长度,甚至是数学操作,例如+ - */
-- Start by creating your array, and another for demonstration purposes
local object = {}
local demo = {1, 2, 3, 4}
-- Create a your metamethods contained in a table
local metamethods = {
__index = function(self, index) return demo[index] end;
__newindex = function(self, index, value) demo[index] = value end;
}
-- Lets print out what is in the object table for demonstration
print(object[1]) -- nil
print(object[2]) -- nil
print(object[3]) -- nil
print(object[4]) -- nil
-- Use the setmetatable(table a, table b) function to set the metamethods
-- stored in 'b' to 'a'
setmetatable(object, metamethods);
-- Lets print out what is in the object table for demonstration
print(object[1]) -- 1
print(object[2]) -- 2
print(object[3]) -- 3
print(object[4]) -- 4
为什么上面的代码有效?当使用索引 __index
(metamethods.__index
) 设置元表时,如果附加表的 (object
) 已建立索引且键为' t 存在 (nil
),那么它将调用指定的函数。在 __index 函数中,它返回演示的表,并将索引直接传递给它。就好像:当您执行 object[1]
时,您实际上是在执行 demo[1]
但当然需要元方法的帮助。这有效地创建了某种代理。
setmetatable() 的一个很酷且快速的用法是它返回您作为第一个参数(表)传递的值。
local object1 = setmetatable({}, { __index = function(self, i) return 1 end })
print(object1["a"]) -- 1
print(object2[321]) -- 1
关于Lua重载运算符来创建假数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75091483/