|
Now IF we only can get the '__len' metamethod to be applied to tables (as it should, in my opinion...). That would allow wrapping the tuples into table-looking proxy, with #t and t[k] working as usual. Current definition: function len_event (op) if type(op) == "string" then return strlen(op) -- primitive string length elseif type(op) == "table" then return #op -- primitive table length else local h = metatable(op).__len if h then -- call the handler with the operand return h(op) else -- no handler available: default behavior error(···) end end end Suggestion: function len_event (op) if type(op) == "string" then return strlen(op) -- primitive string length else local h = metatable(op).__len if h then -- call the handler with the operand return h(op) elseif type(op) == "table" then return #op -- primitive table length else -- no handler available: default behavior error(···) end end end I fail to see a downside on this; it would only 'hurt' people using the '#' operator (an added check for the metamethod even if it's not there), and that exact case seems to be what this whole message thread is about: '#' not being good with holes, and tails. It would also allow people to rewrite '__len' to be always safe, if they choose to. They could only do this for tables they KNOW would be carrying holes, so no extra burden would be implied on the rest of Lua. Small change, can we have that and Shut Up? :! -asko PA kirjoitti 4.8.2007 kello 16:29:
|