访问类型元表Lua [英] Accessing Type Metatables Lua

查看:74
本文介绍了访问类型元表Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很明显,getmetatable可以访问几种类型的元表:

Its obviously that getmetatable can access the metatables of several types:

getmetatable("")
getmetatable({})
getmetatable(newproxy(true))

但是,似乎您无法获得其他类型的元表(不包括函数).似乎无法访问数字,布尔值或nil的元表.

However it appears as though you cannot get the metatable of other types (functions aside). There appears to be no way to access the metatable of numbers, booleans, or nil.

我还想知道是否可以访问整个表类型的元表.为了能够做这样的事情:

I was also wondering if one would be able to access a metatable of the entire table type. To be able to do something like this:

({}) + ({})

推荐答案

默认情况下,数字,布尔值和nil没有元表(因此getmetatable返回nil).您可以用debug.setmetatable给他们一个.

Numbers, Booleans and nil have no metatable by default (hence getmetatable returning nil). You can give them one with debug.setmetatable though.

没有公用表元表. (与用户数据相同(至少种类繁多))

There is no common table metatable. (and same for userdata (at least of the heavy variety))

这篇关于访问类型元表Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆