lua-users home
lua-l archive

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


Or '#' instead of 'end'?
  t[#] means t[#t]
  t[# -1 ] means t[#t -1]
  t[#  + 1 ] = 'syntactic sugar'

Egil


Den 2012-01-19 11:24, skrev Patrick Rapin:
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.