lua-users home
lua-l archive

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


What is the best way to support something akin to table like fields on userdata, so you can have:

foo.status = 201
foo:puts("hello world")

where foo is a userdata?

Using the metatable for adding methods as described in PiL works well for adding methods to it, but providing an __index on the metatable causes __index to be invoked rather than looking up the value in the metatable. The only ~obvious way I see is to maintain a hash of values in the userdata and do all lookups there, but it would be handy to be able to let Lua do that for me. This could be done with an upvalue on __index, but doing the __index against an upvalue seems like a hack.

Anyone have a good solution to this?

Thanks!

-Brian