Re: Small non-bug oddity with table.concat

  • From: Alexander Gladysh <agladysh@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 13 Nov 2012 10:14:36 +0400

On Tue, Nov 13, 2012 at 2:45 AM, David Given <dg@xxxxxxxxxxx> wrote:
> Lua defines arrays to terminate at *any* nil, not just the first one, so
> both 1 and 3 are perfectly valid values for the length of a here. LuaJIT
> picks the first one:

> Stock Lua 5.1.5, however, picks the second one, which then causes
> table.concat() to error out:

Please note that if you'll construct array using a[1] = "foo"; a[2] =
nil and so on, its reported "length" would be different.

Unspecified behaviour is just that âÂunspecified.

> This *isn't* a bug, as both code paths are equally valid given the code
> I gave it. But the two behaviours are sufficiently different that it's
> worth bringing to people's attention.

It is known gotcha in LJ2 (I'd say â in Lua as a language), not
related to table.concat (table.concat just calculates table length).
Do not rely on unspecified behavior in your code.

BTW, if you have good test coverage and your code works both in stock
Lua and LuaJIT, this gotcha becomes a feature â you use it to catch
unspeficied behaviour in your code (which *will* strike you if you use
stock Lua âÂsee above about array construction).

Alexander.

Other related posts: