lua-users home
lua-l archive

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

On Tue, May 19, 2009 at 10:12 AM, Richard Hundt <> wrote:
> To optimise for lookup, we could "flatten" out the meta events, keeping each
> in a separate table, so we don't need to test if we have a function
> "__index" handler or a table:

I don't know if it really matters; it depends whether function
attribute lookup is to be prioritized.

One can do a lot with a simple hook. For instance, in PL I have a
module seq which provides functions that generate iterators (like
random(), range(), list(), etc) and functions which operate on these
iterators.  To do method chains, I've needed a sequence wrapper class.
 But still, something like seq(seq.random(10)):printall() is kinda

A lot can be done with a single statement:

debug.setmetatable(function() end,{__index=seq})

Now seq.random(10):printall() works directly, without a wrapper!
Another example:

> seq.range(1,10):printall()
1 2 3 4 5 6 7
8 9 10


> math.sin:copy()
C:\Program Files\Lua\5.1\lua\pl\seq.lua:153: bad argument #1 to '(for generator)
' (number expected, got nil)

So without individual function metatables, this kind of hack is going
to give weird error messages, if you accidently passed a non-iterator
function to such a chain, because there is no way to know that a
function is intended to be used as an iterator.  I had a brief affair
with 'callable strings' (so e.g. s(1) would be the same as s:sub(1,1))
but man, the error messages are just not worth it.

BTW, the framework you have suggested is still necessary if different
modules want to use this mechanism simultaneously. But name collisions
will happen between different lookup tables!

steve d.