lua-users home
lua-l archive

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


On Feb 22, 2007, at 6:37 PM, William Ahern wrote:
I thought I had spotted a patch awhile ago which came across this list.
It basically allowed for syntax like:

	local t, v;

	v = t.something.innerthing

In this case v would be nil, and the interpreter wouldn't get upset.
Likewise,

	t.something.innerthing = "foo"

would implicitly create the table t.something.

Is there a patch against 5.1 for this? Basically, amongst other less
shallow reasons, I'd much rather be able to do

	proc.slaves.max	= 10
	proc.slaves.min	= 3

rather than

	proc		= {}
	proc.slaves	= { max = 10, min = 3 }

which is still nicer than

	proc	= {
		slaves	= {
			max	= 10,
			min	= 3
		}
	}

Abstractly the namespace of the configuration is hierarchical, but
usually its nice to pretend its flat. And to do that the language needs
to play along.

This doesn't exactly match your needs, but perhaps it's close enough?

Lua 5.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
> autotable = {
>>   meta = {
>>     __index = function(t,k)
>>       local auto = autotable()
>>       t[k] = auto
>>       return auto
>>     end
>>   }
>> }
> function autotable:new( )
>>   local t={}
>>   setmetatable(t,self.meta)
>>   return t
>> end
> setmetatable( autotable, { __call = autotable.new } )

> proc = autotable()
> proc.slaves.max = 10
> proc.slaves.min = 3
> for k,v in pairs( proc ) do print( k,v ) end
slaves  table: 0x30c260
> for k,v in pairs( proc.slaves ) do print( k,v ) end
max     10
min     3
> _ = proc.oops
> for k,v in pairs( proc ) do print( k,v ) end
oops    table: 0x30d080
slaves  table: 0x30c260


As you can see, you a) need to tell it when you want an autotable. (You can't just say "local t" and have dereferencing it automatically make it into t table. That's just crazy talk, IMO.) Also, simply asking for a property necessarily instantiates it.