lua的元表

metatable是Lua中的重要概念,每一个table都可以加上metatable,以改变相应的table的行为。让我们看一个例子:

t = {} -- 普通的table
mt = {} -- metatable
setmetatable(t, mt) -- 设定mt为t的metatable
getmetatable(t) -- 返回mt

使用getmetatablesetmetatable来查看和设定metatable。当然,上面的代码也可以压缩成一行:

t = setmetatable({}, {})

这是因为setmetatable会返回它的第一个参数。

metatable可以包括任何东西,metatable特有的键一般以__开头,例如__index__newindex,它们的值一般是函数或其他table。

t = setmetatable({}, {
  __index = function(t, key)
    if key == "foo" then
      return 0
    else
      return table[key]
    end
  end
})

__index

这是metatable最常用的键了。

当你通过键来访问table的时候,如果这个键没有值,那么Lua就会寻找该table的metatable(假定有metatable)中的__index键。如果__index包含一个表格,Lua会在表格中查找相应的键。

other = { foo = 3 }
t = setmetatable({}, { __index = other })
t.foo -- 3
t.bar -- nil

如果__index包含一个函数的话,Lua就会调用那个函数,table和键会作为参数传递给函数。

__newindex

类似__index__newindex的值为函数或table,用于按键赋值的情况。

other = {}
t = setmetatable({}, { __newindex = other })
t.foo = 3
other.foo -- 3
t.foo -- nil

t = setmetatable({}, {
  __newindex = function(t, key, value)
    if type(value) == "number" then
      rawset(t, key, value * value)
    else
      rawset(t, key, value)
    end
  end
})

t.foo = "foo"
t.bar = 4
t.la = 10
t.foo -- "foo"
t.bar -- 16
t.la -- 100

上面的代码中使用了rawgetrawset以避免死循环。使用这两个函数,可以避免Lua使用__index__newindex

运算符

利用metatable可以定义运算符,例如*

t = setmetatable({ 1, 2, 3 }, {
  __mul = function(t, other)
    new = {}

    for i = 1, other do
      for _, v in ipairs(t) do table.insert(new, v) end
    end

    return new
  end
})

t = t * 2 -- { 1, 2, 3, 1, 2, 3 }

__index__newindex不同,__mul的值只能是函数。与__mul类似的键有:

  • __add (+)
  • __sub (-)
  • __div (/)
  • __mod (%)
  • __unm 取负
  • __concat (..)
  • __eq (==)
  • __lt (<)
  • __le (<=)

__call

__call使得你可以像调用函数一样调用table:

t = setmetatable({}, {
  __call = function(t, a, b, c, whatever)
    return (a + b + c) * whatever
  end
})

t(1, 2, 3, 4) -- 24

这是很有用的特性。需要以直接调用table的形式调用table中的某个(默认)函数的时候,使用__call设定很方便。例如,kikitotween.lua,就用了这个技巧,这样直接调用tween就可以调用tween.start。再如MiddleClass中,类的new方法可以通过直接调用类的方式调用。

__tostring

最后讲下__tostring,它可以定义如何将一个table转换成字符串,经常和print配合使用,因为默认情况下,你打印table的时候会显示table: 0x<16进制数字>

t = setmetatable({ 1, 2, 3 }, {
  __tostring = function(t)
    sum = 0
    for _, v in pairs(t) do sum = sum + v end
    return "Sum: " .. sum
  end
})

print(t) -- prints out "Sum: 6"

例子

综合运用以上知识,我们编写一个2D矢量类:

Vector = {}
Vector.__index = Vector

function Vector.__add(a, b)
  if type(a) == "number" then
    return Vector.new(b.x + a, b.y + a)
  elseif type(b) == "number" then
    return Vector.new(a.x + b, a.y + b)
  else
    return Vector.new(a.x + b.x, a.y + b.y)
  end
end

function Vector.__sub(a, b)
  if type(a) == "number" then
    return Vector.new(b.x - a, b.y - a)
  elseif type(b) == "number" then
    return Vector.new(a.x - b, a.y - b)
  else
    return Vector.new(a.x - b.x, a.y - b.y)
  end
end

function Vector.__mul(a, b)
  if type(a) == "number" then
    return Vector.new(b.x * a, b.y * a)
  elseif type(b) == "number" then
    return Vector.new(a.x * b, a.y * b)
  else
    return Vector.new(a.x * b.x, a.y * b.y)
  end
end

function Vector.__div(a, b)
  if type(a) == "number" then
    return Vector.new(b.x / a, b.y / a)
  elseif type(b) == "number" then
    return Vector.new(a.x / b, a.y / b)
  else
    return Vector.new(a.x / b.x, a.y / b.y)
  end
end

function Vector.__eq(a, b)
  return a.x == b.x and a.y == b.y
end

function Vector.__lt(a, b)
  return a.x < b.x or (a.x == b.x and a.y < b.y)
end

function Vector.__le(a, b)
  return a.x <= b.x and a.y <= b.y
end

function Vector.__tostring(a)
  return "(" .. a.x .. ", " .. a.y .. ")"
end

function Vector.new(x, y)
  return setmetatable({ x = x or 0, y = y or 0 }, Vector)
end

function Vector.distance(a, b)
  return (b - a):len()
end

function Vector:clone()
  return Vector.new(self.x, self.y)
end

function Vector:unpack()
  return self.x, self.y
end

function Vector:len()
  return math.sqrt(self.x * self.x + self.y * self.y)
end

function Vector:lenSq()
  return self.x * self.x + self.y * self.y
end

function Vector:normalize()
  local len = self:len()
  self.x = self.x / len
  self.y = self.y / len
  return self
end

function Vector:normalized()
  return self / self:len()
end

function Vector:rotate(phi)
  local c = math.cos(phi)
  local s = math.sin(phi)
  self.x = c * self.x - s * self.y
  self.y = s * self.x + c * self.y
  return self
end

function Vector:rotated(phi)
  return self:clone():rotate(phi)
end

function Vector:perpendicular()
  return Vector.new(-self.y, self.x)
end

function Vector:projectOn(other)
  return (self * other) * other / other:lenSq()
end

function Vector:cross(other)
  return self.x * other.y - self.y * other.x
end

setmetatable(Vector, { __call = function(_, ...) return Vector.new(...) end })

原文 Lua Metatables Tutorial
翻译 SegmentFault


SegmentFault_行业快讯
第一时间为开发者提供行业相关的实时热点资讯

思否编辑部官方账号,欢迎私信投稿、提供线索、沟通反馈。

4k 声望
116.9k 粉丝
0 条评论
推荐阅读
高效开源的网络扫描框架 —— NINJA-PingU
NINJA-PingU作为一个扫描框架,可不仅仅只有ping这样简单的功能,他是一个专门为大型网络扫描所设计的框架,兼顾效率的同时,支持插件的开发。

思否编辑部阅读 4.5k

Redis分布式锁的实现
很多新手将 分布式锁 和 分布式事务 混淆,个人理解:锁 是用于解决多程序并发争夺某一共享资源;事务 是用于保障一系列操作执行的一致性。我前面有几篇文章讲解了分布式事务,关于2PC、TCC和异步确保方案的实现...

KerryWu4阅读 7.2k评论 2

初见 AI,你会感兴趣的知识
人工智能(Artificial Intelligence,简称AI)是指计算机系统通过仿效人的思维和行为方式,实现类似于人类智能的一种技术。20世纪初期,“人工智能”就作为一个概念被提出。当时,科学家们开始思考如何使机器能够模...

布鲁瓦丝1阅读 543

一文讲透 Redis 事务 (事务模式 VS Lua 脚本)
准确的讲,Redis 事务包含两种模式 : 事务模式 和 Lua 脚本。先说结论:Redis 的事务模式具备如下特点:保证隔离性;无法保证持久性;具备了一定的原子性,但不支持回滚;一致性的概念有分歧,假设在一致性的核心...

勇哥java实战分享阅读 540

封面图
什么是 LuaJIT?为什么 Apache APISIX 选择了 LuaJIT?
简单地说,LuaJIT 是 Lua 这种编程语言的实时编译(JIT,Just-In-Time Compilation)器的实现。对于不太了解 LuaJIT 的读者,我们可以将 LuaJIT 拆成 Lua 和 JIT 两个部分来理解。

API7_技术团队阅读 474

TMP的阴影性能如何
1)TMP的阴影性能如何​2)CommandBuffer.DrawMeshInstanced无法画阴影问题3)Unity编辑器在Require大量加载Lua文件时,经常报出not enough memory4)场景制作的时候,2D资源受后处理调色影响比较大

侑虎科技阅读 203

封面图
Unity中级客户端开发工程师的进阶之路
上期UWA技能成长系统之《Unity高级客户端开发工程师的进阶之路》得到了很多Unity开发者的肯定。通过系统的学习,可以掌握游戏性能瓶颈定位的方法和常见的CPU、GPU、内存相关的性能优化方法。

侑虎科技阅读 136

封面图

思否编辑部官方账号,欢迎私信投稿、提供线索、沟通反馈。

4k 声望
116.9k 粉丝
宣传栏