lua-users home
lua-l archive

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


On Mon, Jun 21, 2010 at 8:54 PM, Matthew Wild <mwild1@gmail.com> wrote:
> I have often had to write loops over tables (often producing new tables) running tostring() over
> all values before passing to table.concat, just to make sure it won't
> throw an error.

That idiom comes up at times in my code:

  local ts = {}
  for i=1,#ts do ts[i] = tostring(t[i]) end
  local s = table.concat(ts, c)

One occurrence is when implementing a __tostring method of a tuple
ADT, where the tuple is implemented as a table and can contain
arbitrary values, including nils.  However, for strings I replace that
tostring(t[i]) with string.format("%q",t[i]) since otherwise strings
will be indistinguishable from non-strings in the tuple.  This doesn't
fit into proposal for table.concat supporting __tostring though.