[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Setting the __mode metatable field
- From: Mark Hamburg <mhamburg@...>
- Date: Tue, 04 Jan 2005 07:54:19 -0800
I've been considering adding standard global utilities for these to my
project, but they really seem like something worth establishing general
standards for.
Straw man proposal:
local modemetas = {}
function table.make( mode )
local result = {}
if mode then
local meta = modemetas[ mode ]
if not meta then
meta = { __mode = mode }
modemetas[ mode ] = meta
end
setmetatable( result, meta )
end
return result
end
Usage:
weakTable = table.make( "kv" )
Mark
on 1/4/05 5:43 AM, Mike Pall at mikelu-0501@mike.de wrote:
> Hi,
>
> David Jones wrote:
>> t={}
>> setmetatable(t,{})
>> getmetatable(t).__mode='v'
>
> I think the shortest (and proper) way to do this is:
>
> local t = setmetatable({}, {__mode='v'})
>
> If you have many of these tables it is worth sharing the metatable:
>
> local weakvmeta = {__mode='v'}
> local function weakvtable(t) return setmetatable(t or {}, weakvmeta) end
>
> local t1 = weakvtable()
> local t2 = weakvtable{foo=a, bar=b} -- example table initializers
> ...
>
> Bye,
> Mike