lua-users home
lua-l archive

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


Jamie Webb wrote:
On Wed, Aug 18, 2004 at 05:05:19PM -0700, Marc Nijdam wrote:
  
a = {
  b = {};
  [b.c] = 22
}


which give "b not defined globally". Placing 'a' in front of this shows 
why this is declarative.. a is not defined yet.
    
Also, the above code means something quite different to what you were trying
to achieve, I think. To understand, try this example:

x = { y = "z" }
a = { [x.y] = 22 }

The effect is the same as:

a = { z = 22 }

or:

a = { ["z"] = 22 }

This is, the _expression_ inside the square brackets is evaluated, and
the resulting value is used as the table key. No change is made to the
'x' table in this instance.

It's perhaps unfortunate that similar syntax is used for an assignment
statement in normal code, and for a key-value pair in a table
constructor. They really are quite distinct things.
  
Makes sense now, thanks for the explanation.

To be more specific about why I'm trying to do this:

My application has a nested set of tables that form the "prototype" of a set of configuration items. What I'm trying to do is "clone" the prototype and override values in one of the subtables in the clone. so

a = {
    b = {
       c = 22;
    }
}

d = clone{a,
   b.c = 33;
}

would have been the cleanest, since it states that I want to clone a and overide b.c in the clone. What I need to do now is:

d = clone(a);
d.b.c = 33;

which works, except that nesting is quite deep and this gets real repetitive. I'm trying not to turn the configuration "language" into a full prototypical programming language since it needs to be editable by non-programmers.

Any ideas welcome...

--Marc