lua-users home
lua-l archive

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


On Thu, Dec 13, 2012 at 9:47 AM, Wesley Smith <wesley.hoke@gmail.com> wrote:
> Couldn't you just use a table as a proxy for userdata via __index?
> It's not ideal to have to proxy, but you could easily make a wrapper
> function so that you can pretend it's not happening :)

That does work - gsub respects __index, which is nice (I had to check this)

Best global solution in such a situation is the following monkey patch:

local gsub,type = string.gsub,type

function string.gsub(s,pat,repl)
   if type(repl) == 'userdata' then
      local obj = repl
      repl = function(...) return obj(...) end
   end
   return gsub(s,pat,repl)
end