What is newproxy and how is it useful?

I messed with Lua yesterday and stumbled upon the newproxy function.

http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#newproxy

I understand this, but I'm not sure how useful this is. I know that it creates an empty userdata object with an attached metathe (if the argument is true).

How useful is newproxy to it? Here is an example of what I did while messing with it:

local proxy = newproxy(true)
local metatable = getmetatable(proxy)

metatable.__index = function(array, key) print(array, key) end

local y = proxy[100]

--[[
    OUTPUT:
        userdata: 0x443ad4b4 100
]]
+4
source share
1 answer

See this related SO question and answer. Empty userdata were useful for detecting when GC reclaimed objects. Zero-sized tables in Lua 5.2 can play this role.

0
source

All Articles