lua-users home
lua-l archive

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


i am wondering if the table constructor syntax could be improved:

currently a table constructor is seperated in 2 parts: the lfieldlist
(vector-like constructor) and the ffieldlist (map-like constructor)
if a constructor contains both vector-like and map-like constructor parts,
they must be separated with a semicolon:
t = {1,2;x=1,y=2}

i find this not intuitive, i'd rather like to write:
t = {1,2,x=1,y=2}

or to go even further, i'd like to be able to mix the two, like this:
t = {1,2,x=1,y=2,3,4} -- equivalent to: {1,2,3,4;x=1,y=2}

and one more thing that would be nice would be to specify where a list
should start: (somehow like c-enums)
t = {[97]="a","b","c","d","e"} -- equivalent to:
{[97]="a",[98]="b",[99]="c",[100]="d",[101]="e"}

to achieve both, assume that if no explicit index is given, the last numeric
index + 1 should be used.

for backwards compatibility, ";" could be treated equivalent to "," so all
those forms would be valid:
t = {1,2,3}
t = {1;2;3}
t = {1,2;n=2}
t = {1,2,;n=2,}

another nice thing would be to make the separator optional (like for blocks
of statements):
t = {1 2 3}
t = {
	1
	2;
	x=5,
}

this would enable you to easily transform a list of global variable
initializers into a table just by adding curly braces:

global_var1 = "a"
global_var2 = 2; global_func = print;

-->

local my_vars = {
global_var1 = "a"
global_var2 = 2; global_func = print;
}

what do you think?

i have no idea if this would complicate the parser too much to be feasible.

Cheers,
Peter