lua-users home
lua-l archive

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


On Jul 6, 2013 3:06 PM, "Jorge" <xxopxe@gmail.com> wrote:
>
> On 06/07/13 13:05, Tim Hill wrote:
>>
>> Exactly!!
>
>
> Even with that explicit length, you can not have a nil inside a string. That would not make any sense. After all, a string is a array of char! :)
>
> What I would find silly is the following:
>
>  --compute the sum of an array:
> local sum=0
> for i=1, #a do
>     if a[i] then sum=sum+a[i]
> end
>
> An array is a very regular structure. This allows for compact agorithms. Having holes in it defeats this. If it has holes, it is not an array, it's a dictionary, and you use other algorithms.

Adding nil doesn't change anything here: your loop would also break if some confused person stuck a table or a coroutine into the array. It's already necessary to either check that each value is the type you expect or ensure that unexpected types don't end up in the array to begin with, whether nil is an accepted type or not.