Re: length operator for the arrays

  • From: Long Cheng <long.x.cheng@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 6 May 2016 23:17:01 +0800

That's not where this ambiguity usually happens. Apart from aforementioned 
situation with
arguments of the function, most of the times where people relied on this 
behavior is something
like this:

t = {<dynamic value>, <dynamic value calculated as nil>, <another dynamically 
calculated value>}

i.e. the table is strictly formed as an array (no explicit keys), but some of 
the values are
dynamically calculated as nil, leaving "holes".

Then this table is passed somewhere, where it is treated with assumption that 
it is an array,
including inserting new elements (expecting that they will be added to the 
end), calculating
the length of the array (with the length being an important information), etc.

In this situation the behavior always was consistent and predictable.

Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> t = {1,nil,2,nil}
> print(#t)
1
>

Are your sure this behavior is what you want?

Regards
Long

Other related posts: