lua-users home
lua-l archive

Re: Best way to have table-like fields on userdata

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


On Tue, Nov 28, 2006 at 10:28:17AM -0800, Brian McCallister wrote:
> 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")
I made foo a table, and having a table mapping from tables to their
corresponding (now light) userdatas. That way, foo is a real table,
not something pretending to be one--pairs() and the "function foo:x() end"
syntax works. It does result in more dispatch overhead, since C
functions bound to it need to look up the userdata.
I don't actually use this very much, and may switch to a userdata-
with-table-stashed approach at some point to reduce that overhead,
maybe with table creation being lazy, so I don't pay the memory price
of the table for small objects that never use it.
-- 
Glenn Maynard

AltStyle によって変換されたページ (->オリジナル) /