lua-users home
lua-l archive

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


> This may have been discussed before, but I couldn't easily 
> find it in the archives.
> 
> This is illegal in Lua 5:
> 
> 	t = { 1="hey", 2="ho" }
> 
> It must be written like this:
> 
> 	t = { [1]="hey", [2]="ho" }
> 
> The question is why.  I assume there is some ambiguity that 
> is being addressed here, but I can't see it.  


I assume the reason is to do with the way syntactic sugar works and consistency, 

	a.b === a['b']  -->  a = { b = 'value' }  -- works

i.e. b is a string. From the manual, about table constructors:

	field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp

This is just consistent with syntactic sugar. If you allowed

	a.1 === a[1]  -->  a = { 1 = "value" }  -- doesn't work

a.1 could be mistaken for an erroneous floating point number.


?
Nick