Lua rawget rawset newindex 函数定义和例子

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/10032249.html



在绝大多数情况下,我们都不会用到rawget和rawset。

本文的运行环境:lua 5.3 for windows

rawset 赋值操作#

rawset是在设置值的过程,进行处理,比如:当某个值改变时,触发事件。或修改某个key为新值。

来看看rawset函数的定义

Copy
--- Sets the real value of `table[index]` to `value`, without invoking the --- `__newindex` metamethod. `table` must be a table, `index` any value --- different from **nil** and NaN, and `value` any Lua value. ---@param table table ---@param index any ---@param value any function rawset(table, index, value) end

看个例子,设置过__newindex之后,就不会调用__index了?

Copy
local tb = {} setmetatable(tb, { __index = function() return "not find" end }) setmetatable(tb, { __newindex = function(table, key, value) local patchKey = "version" if key == patchKey then rawset(table, patchKey, "补丁值") else rawset(table, key, value) end end }) tb.version = "正常版本" tb.date = "2018" print(tb.version) --打印 补丁值 print(tb.server) --打印nil,不会调用__index方法了? print(tb.date) --打印2018

经过我的测试后, 发现

Copy
---如果把__index放在__newindex之后,调用不存在值,才会调用__index方法 如果在__index__newindex之前,则不会调用

rawget 取原始值#

rawget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效

来看看rawget函数的定义

Copy
--- Gets the real value of `table[index]`, the `__index` metamethod. `table` --- must be a table; `index` may be any value. ---@param table table ---@param index any ---@return any function rawget(table, index) end

编写一个例子,测试rawget绕过__index方法

Copy
local tb = {} setmetatable(tb, { __index = function() return "not find" end }) tb.version = "正常版本" print(tb.version) print(tb.server) ---不存在的值,调用__index方法 --rawget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效 print(rawget(tb, "version")) --打印 正常版本 print(rawget(tb, "server")) --打印nil

__newindex#

__newindex可以和rawset配合使用,也可以单独使用

当为表分配值时,解释器会查找__newindex方法,如果存在,则解释器会调用它。

结合使用 __index和 __newindex,允许lua有强大的构造,从只读表,到具有默认值的表,到面向对象编程的继承

文档:https://www.lua.org/pil/13.4.2.html

Lua5.3 __index要通过setmetatable设置#

在lua5.3中,直接使用tableA.__index = function() end 设置,我这边测试,并不会生效

Copy
local tempTable = { memberB = "test" } tempTable.__index = function() return "not find" end print(tempTable.memberA) --打印 nil print(tempTable.memberB) --打印test

而通过这种方式就正常

Copy
local tempTable = { memberB = "test" } ---__index定义了当key查找不到的行为 setmetatable(tempTable, { __index = function() return "not find" end }) print(tempTable.memberA) --打印 not find print(tempTable.memberB) --打印test
作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(2672)  评论(5编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
CONTENTS
点击右上角即可分享
微信分享提示