您的位置:首页 > 编程语言 > Lua

lua函数之rawget和rawset

2017-12-27 15:05 429 查看
rawset/rawget:对”原始的”表进行直接的赋值/取值操作。当操作table时,如果我们有以下需求:

访问时,不想从 __index 对应的元方法中查询值

更新时,不想执行 __newindex 对应的元方法

在 __newindex 元方法中,设置table的key/value时,不想陷入死循环而爆栈

元表(metatable)

在将rawset和rawget之前,我们先来看看matetable。Lua中每种类型的值都有都有他的默认操作方式, 如, 数字可以做加减乘除等操作, 字符串可以做连接操作, 函数可以做调用操作, 表可以做表项的取值赋值操作. 他们都遵循这些操作的默认逻辑执行, 而这些操作可以通过Metatable来改变。这里我只讲table的mettable。 而__index和__newindex与rawget和rawset关系比较密切。如下:

t = {} -- 普通表
mt = {} -- 元表,现在暂时什么也没有
setmetatable(t, mt) -- 把mt设为t的元表
getmetatable(t) -- 这回返回mt


__index方法

元表里最常用的索引可能是__index,它可以包含表或函数。当你通过索引来访问表, 不管它是什么(例如t[4], t.foo, 和t[“foo”]), 以及并没有分配索引的值时,

Lua 会先在查找已有的索引,接着查找表的metatable里(如果它有)查找__index 索引。 如果__index 包含了表, Lua会在__index包含的表里查找索引。 举例如下:

1.__index为表
other = { foo = 3 }
t = setmetatable({}, { __index = other })
print("value foo = ", tab_1["foo"])   -- 0
print("value moo = ", tab_1["moo"])   -- nil
2.__index为函数
tab_1 = setmetatable({}, {
__index = function(t, key)
if key == "foo" then
return 0
else
return table[key]
end
end
})
print("value foo = ", tab_1["foo"])   -- 0
print("value moo = ", tab_1["moo"])   -- nil


__newindex方法

和__index类似,可以包含函数和表.当你给表中不存在的值赋值时,Lua会在metatable里查找__newindex,调用顺序和 __index一样。举例如下:

1.__newindex为表
local other = {}
local tab = setmetatable({}, { __newindex = other })
tab.c = 3
print("value tab.c = ", tab.c) -- nil
print("value other.c = ", other.c) -- 3
2.__newindex为函数
local tab_2 = setmetatable({},{
__newindex = function(table, key, value)
table.key = "yes,i am"
end
})
tab_2.c = 3  -- 报错,stack overflow
3.__newindex为rawset函数
local tab_3 = setmetatable({},{
__newindex = function(table, key, value)
rawset(table, key, value)
end
})
tab_3.a = 11;
print("value a = ", tab_3["a"])


rawget(table, index)

根据参数table和index获得真正的值table[index],也就是说根本不会调用到元表,其中参数table必须是一个表,而参数index可以使是任何值。举例如下:

local tab = {
a = 'a',
b = 'b',
c = 'c'
}
local mt = {
x = 1,
y = 2
}
print("value tab.c = ", tab.c) --c
print("value tab.x = ", tab.x)   -- nil
setmetatable(tab, {__index = mt}) --设元表
print("metatable value tab.c = ", tab.c)   --c
print("metatable value tab.x = ", tab.x)   -- 1
print("not use metatable value tab.c = ", rawget(tab,"c"))   -- c
print("not use metatable value tab.x = ", rawget(tab, "x"))   -- nil


rawset(table, index, value)

在不调用元表的情况下,给table[index]赋值为value,其中参数table必须是一个表,而参数index可以是不为nil的任何值。举例如下:

local tab_3 = setmetatable({},{
__newindex = function(table, key, value)
rawset(table, key, value)
end
})
tab_3.a = 11;
print("value a = ", tab_3["a"])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua rawget rwaset