lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> The purpose of this value is to encapsulate other values
> similar to using an empty table with setmetatable ...
> the proxied value is passed to metamethods instead of the proxy
the implementation is shorter than the suggestion, and doesn't require any changes to the core language, let alone a new type.

local proxied,metatable = {},{} -- symbols to prevent namespace conflict
local Proxy = {} -- class
loop(function (meta)
  Proxy[meta] = function (self, ...)
    local method = self[metatable][meta]
    if method then return method(self[proxied], ...); end
  end
end) "__call" "__add" "__sub" "__mul" -- etc
function envelop (p,m)
  return setmetatable({[proxied]=p, [metatable]=m}, Proxy)
end