Created
February 2, 2019 21:29
-
-
Save porglezomp/e198004a7954528f4b339db0dac8823c to your computer and use it in GitHub Desktop.
Metatable on Metatables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local x = { value = 42 } | |
local index = { | |
__index = function(t, k) | |
print(k) | |
if k == "__add" then | |
return function(l, r) | |
return { value = l.value + r.value } | |
end | |
end | |
end | |
} | |
setmetatable(index, index) | |
setmetatable(x, index) | |
index.__add -- works fine, gives the right add | |
x.__add -- works fine, gives the right add | |
x + x -- fails, presumably the metatable lookup uses primitive indexing and skips meta indexing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment