Skip to content

Instantly share code, notes, and snippets.

@porglezomp
Created February 2, 2019 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porglezomp/e198004a7954528f4b339db0dac8823c to your computer and use it in GitHub Desktop.
Save porglezomp/e198004a7954528f4b339db0dac8823c to your computer and use it in GitHub Desktop.
Metatable on Metatables
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