lua-users home
lua-l archive

Re: t[] = 'syntactic sugar'

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


That would be handy, especially when the table name is not just "t",
but a complex expression like "my_object.get_table()".
However I don't really like the syntax, which looks incomplete.
It could be better expressed as :
 t[end] = 'syntactic sugar'
Because "end" is a keyword, there is no ambiguity in the grammar, and
the intention is more clear.
In MATLAB, "end" can be used in a similar but more general way. It
represent the current length in the current dimension.
Examples in this language :
 a = [11 12 13; 21 22 23]
 a =
 11 12 13
 21 22 23
 a(end)
 ans = 23
 a(end, end-1)
 ans = 22
 a(end+1,end)=33
 a =
 11 12 13
 21 22 23
 0 0 33
If we like that feature and want it in Lua also, the syntactic sugar
would become a bit longer :
 t[end+1] = 'syntactic sugar'
for appending, but now allows for example
 print( t[end] )
to display the last argument, or
 t[end], t[end-1] = t[end-1], t[end]
to swap the last two values.

AltStyle によって変換されたページ (->オリジナル) /