lua-users home
lua-l archive

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


On 2/8/2013 1:37 PM, Ivo Beltchev wrote:
I’d like to know in what cases I can count on deterministic order of table iteration. I’m pretty sure if the keys are objects that are stored in memory (like tables, threads, closures, or full user data), the order is not deterministic. But what if the keys are limited to simple types, like numbers, strings, booleans, light user data? Is the order going to be always the same?

The order isn't defined unless you use contiguous integer keys starting from 1 AND you use ipairs() to iterate. Relying on a deterministic order otherwise is asking for trouble.

In reality, the order isn't going to change if the table doesn't change. But if you insert a key, the new order could be completely different.

And I've also seen the order for the exact same set of simple keys be different depending on the order of insertion into a table -- but my first point stands, that it's UNDEFINED behavior, and shouldn't be relied on.

Tim