lua-users home
lua-l archive

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


> > True, I forgot about that. Is there any chance you could register a C
> > function that uses the API to give you a "dummy" full userdata (one
> > byte long or something) instead of a table, and set the metatable on
> > that instead?
> 
> The (undocumented) function newproxy() might be the ticket. Search the
> list archives.

Try this:

newf = function(f, l)
  local p = newproxy(true)
  local mt = getmetatable(p)
  mt.__call = function(_, ...) return f(...) end
  mt.__len = function() return l end
  return p
end

> f1 = newf(math.cos, 1)
> print(#f1) --> 1
> print(f1(1)) --> 0.54030230586814
> f2 = newf(function(x,y) return x+y end, 2)
> print(#f2) --> 2
> print(f2(2,3)) --> 5

Cheers,
Luis.

-- 
A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>