一、Table
Table實(shí)際上是數(shù)組和哈希表的混合體。當(dāng)保存以連續(xù)整數(shù)為索引的內(nèi)容時(shí),使用數(shù)組結(jié)構(gòu);而對于離散索引及鍵值對,則使用哈希表存儲。這樣做對于空間節(jié)省和查找效率都是有好處的,舉例:
tt = {"a" , "b" , "c"}
print(#tt) --> 3
tt[100] = "d"
print(#tt) --> 3 (100-"d"鍵值對放入哈希表)
tt[4] = "e"
print(#tt) --> 4
二、Closure
Lua的變量引用規(guī)則造成了閉包的出現(xiàn),在Lua中一個(gè)復(fù)雜數(shù)據(jù)結(jié)構(gòu)(table、function)被賦予一個(gè)變量時(shí),變量僅僅拿取其引用。試考慮下面代碼片段:
local f1 = function() ... end
local f2 = function() ... end
local f3 = f1
判斷f1與f2是否相等,得到的結(jié)果為假,而f3與f1則為真。這是因?yàn)閒1與f2引用了兩個(gè)不同的函數(shù)chunk(也可稱為“定義”),而f1直接將其引用(可以看成C/C++中的地址)賦給f3,故而相等。
理解了上面問題,再看看下面的一個(gè)函數(shù):
function createCounter()
local x = 0
return function()
x = x + 1
print(x)
end
end
那么,下面c1與c2相等嗎?
c1 = createCounter()
c2 = createCounter()
由于createCounter中的return部分創(chuàng)建了一個(gè)新函數(shù),因此c1和c2引用的函數(shù)并不相同。因?yàn)閞eturn的匿名函數(shù)中使用到了外部函數(shù)(即createCounter)的局部變量x,所以Lua必須要在某個(gè)地方保存這個(gè)變量,又因?yàn)槊看畏祷氐亩际且粋€(gè)新函數(shù),所以Lua不能共享createCounter中的x,進(jìn)而Lua必須為每個(gè)新函數(shù)分配一個(gè)保存x的地方,這個(gè)問題導(dǎo)致了閉包概念的產(chǎn)生。
通過上面分析,可以明白是語言機(jī)制導(dǎo)致了閉包,而絕非Lua作者故意制造出這么一個(gè)看似有點(diǎn)神秘的東西。
下面給出閉包的定義:一個(gè)保留了創(chuàng)建時(shí)上下文的函數(shù)。而upvalue的定義是:被閉包訪問的保留變量。實(shí)際上所有在Lua中創(chuàng)建的函數(shù)都是閉包,在許多情況下僅有一個(gè)原型的閉包存在,因?yàn)楹瘮?shù)塊只執(zhí)行了一次(也可以說定義了一次)。
下面代碼用閉包實(shí)現(xiàn)了一個(gè)對象工廠:
local function CounterFactory()
local x = 0
return
{
Increase = function()
x = x + 1
end ,
Decrease = function()
x = x - 1
end ,
GetValue = function()
return x
end
}
end
local c1 = CounterFactory()
local c2 = CounterFactory()
c1.Increase()
c1.Increase()
print(c1.GetValue())
三、Tail Call
首先需要明確What is ‘Tail Call’?——其形式為“return f(…)”,return語句中的函數(shù)調(diào)用僅限于單一調(diào)用,涉及函數(shù)返回值運(yùn)算的復(fù)合表達(dá)式不是尾調(diào)用,如:return i * f(i - 1)。由定義可以看出尾調(diào)用實(shí)際上是對位于返回語句中的函數(shù)調(diào)用的優(yōu)化。通常調(diào)用一個(gè)函數(shù)都需要將其上下文入棧,然后再進(jìn)入被調(diào)函數(shù)。在Lua中執(zhí)行符合尾調(diào)用形式的函數(shù)時(shí),因?yàn)樵摵瘮?shù)已經(jīng)沒有代碼需要執(zhí)行了,故作者作了不保存上下文的優(yōu)化。這樣帶來的好處是:對于精心構(gòu)建的遞歸調(diào)用,不需要有棧的消耗,因而絕對不會出現(xiàn)overflow,看看下面遞歸調(diào)用產(chǎn)生的死循環(huán):
local function f(n)
local i = n + 1
print(i)
return f(i) -- tail call
end
f(0)
四、Metatable
Metatable常用來擴(kuò)展table的行為,通過setmetatable(table , metatable)將metatable掛接到table上,很多開發(fā)者用該特性來實(shí)現(xiàn)面向?qū)ο蟮脑O(shè)計(jì)。Metatable中提供了Metamethods,這些方法都以雙下劃線__開頭,如:
__add , __call , __concat , __div , __eq , __index...
以上Metamethods中,__index被調(diào)用的條件是:key不在對應(yīng)的table中。利用該特性可以實(shí)現(xiàn)類-對象、繼承等面向?qū)ο蟾拍睿旅媸莻€(gè)簡單例子:
local Actor = {}
Actor.name = "unnamed"
Actor.hp = 100
Actor.dead = false
Actor.level = 1
Actor.type = "unknown"
function Actor:Say(text)
print(self.name..":"..text)
end
function Actor:IsDead()
return self.dead
end
local AttackableActor = setmetatable({ } , { __index = Actor })
AttackableActor.damage = 20
function AttackableActor:ReceiveHit(attacker)
self:Say("I'm being attacked by "..attacker.name)
self.hp = self.hp - attacker.damage
if self.hp <= 0 then
self:Say("I'm died :(")
self.dead = true
else
self:Say("My HP is "..self.hp)
end
end
function AttackableActor:Attack(target)
if target.dead == true then
self:Say("Oh, "..target.name.." has been dead!")
else
self:Say("I'm is attacking "..target.name)
target:ReceiveHit(self)
end
end
function CreateMonster(name , damage)
local m = setmetatable({ } , { __index = AttackableActor})
m.name = name
m.damage = damage
m.type = "monster"
return m
end
function CreatePlayer(name , damage)
local p = setmetatable({ } , { __index = AttackableActor})
p.name = name
p.damage = damage
p.type = "player"
return p
end
math.randomseed(os.time())
local boss = CreateMonster("Kerrigan" , math.floor(100 * math.random()))
local player = CreatePlayer("Raynor" , math.floor(100 * math.random()))
while player:IsDead() == false and boss:IsDead() == false do
player:Attack(boss)
boss:Attack(player)
end
五、Performance Tips
請參考Lua Performance Tips
六、總結(jié)
Lua是一門語法簡潔,用法靈活的動態(tài)語言。在全面掌握之后,需悉心使用乃可構(gòu)造出簡潔高效的代碼。