当前位置: 首页 > news >正文

Lua rawget rawset newindex 函数定义和例子

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

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

rawset 赋值操作

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

来看看rawset函数的定义

--- 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了?

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

经过我的测试后, 发现

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

rawget 取原始值

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

来看看rawget函数的定义

--- 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方法

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 设置,我这边测试,并不会生效

local tempTable = { memberB = "test" }

tempTable.__index = function()
    return "not find"
end

print(tempTable.memberA) --打印 nil
print(tempTable.memberB) --打印test

而通过这种方式就正常

local tempTable = { memberB = "test" }
---__index定义了当key查找不到的行为
setmetatable(tempTable, { __index = function()
    return "not find"
end })

print(tempTable.memberA) --打印 not find
print(tempTable.memberB) --打印test

转载于:https://www.cnblogs.com/zhaoqingqing/p/10032249.html

相关文章:

  • Python学习手册之正则表达式和元字符
  • 如何用纯 CSS 创作一个变色旋转动画
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 使用 Fastlane 实现 iOS 跟 Android 自动打包脚本
  • Python练习-迭代-2018.11.28
  • 武汉区块链软件技术公司:艺术市场如何从区块链中受益?
  • JAVA入门到精通-第26讲-异常
  • Elasticsearch实践(四):IK分词
  • Alpha 冲刺 (10/10)
  • 汉诺塔解析(图解)
  • Go 基础(非常基础)
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • 微服务架构介绍及开源框架
  • 【345】机器学习入门 - 李宏毅机器学习笔记
  • 动态删边SPFA: [HNOI2014]道路堵塞
  • 自己简单写的 事件订阅机制
  • @jsonView过滤属性
  • HTTP请求重发
  • input的行数自动增减
  • JavaScript 奇技淫巧
  • Java应用性能调优
  • Spring Cloud中负载均衡器概览
  • Spring框架之我见(三)——IOC、AOP
  • Storybook 5.0正式发布:有史以来变化最大的版本\n
  • Twitter赢在开放,三年创造奇迹
  • vue-loader 源码解析系列之 selector
  • webpack+react项目初体验——记录我的webpack环境配置
  • 初识 beanstalkd
  • 关于extract.autodesk.io的一些说明
  • 你真的知道 == 和 equals 的区别吗?
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 事件委托的小应用
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 项目管理碎碎念系列之一:干系人管理
  • 异步
  • linux 淘宝开源监控工具tsar
  • PostgreSQL之连接数修改
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • ​ArcGIS Pro 如何批量删除字段
  • ​批处理文件中的errorlevel用法
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • # 达梦数据库知识点
  • #Linux(Source Insight安装及工程建立)
  • (1)SpringCloud 整合Python
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (补)B+树一些思想
  • (翻译)terry crowley: 写给程序员
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (附源码)计算机毕业设计高校学生选课系统
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (四)linux文件内容查看
  • (转)setTimeout 和 setInterval 的区别
  • (转)Unity3DUnity3D在android下调试