我無法理解爲什麼會出現在這些之間的__index
元方法的行爲,以實例的差異:的Lua元表不一致
A = { __index = A }
function A:speak()
print("I'm an A")
end
An_A = setmetatable({},A)
An_A:speak()
會引發以下錯誤:lua: l.lua:8: attempt to call method 'speak' (a nil value)
雖然
B = { __index = function(t,key) return B[key] end }
function B:speak()
print("I'm an B")
end
An_B = setmetatable({},B)
An_B:speak()
將按預期執行,輸出I'm an B
。
在試圖理解爲什麼這是我讀PIL的this部分的情況。它指出:
The use of the __index metamethod for inheritance is so common that Lua provides a shortcut. Despite the name, the __index metamethod does not need to be a function: It can be a table, instead. When it is a function, Lua calls it with the table and the absent key as its arguments. When it is a table, Lua redoes the access in that table.
我對此的理解是,在涉及片段「A」,__index = A
導致表A
做接入(按照上述報價的boldened segmenet)。如果是這種情況,我不明白爲什麼沒有找到與鍵"speak"
相關的功能。爲了嘗試解決這個問題,我決定在B
片段中實現函數方法,該片段返回B
中與key
相關的值,並且它工作正常。當然__index = A
和(改編自B
)__index = function(t,key) return A[key] end
具有相同的效果。
任何澄清將不勝感激。
謝謝你的偉大的解釋,並沒有出現我這種情況會發生,我曾以爲它會像其他一些語言,其中一些諸如'F = lambda n:f(n)'是有效的。歡呼:) – HennyH 2013-05-11 02:54:01
@HennyH:'f =函數(n)返回f(n)結束'在盧阿罰款也是出於同樣的原因。相當於失敗的python是'd = {「__index」:d}',但python會'NameError' – Eric 2013-05-12 16:30:27
@Eric我想任何遞歸函數都遵循相同的模式。 – HennyH 2013-05-12 22:55:57