lua-users home
lua-l archive

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

On Wed, Jul 20, 2022 at 8:50 PM Gé Weijers wrote:
local last_key = -1

local meta = {
    __index = function(meta, key)
        if key > last_key then
            last_key = key
            return "a"
            return "bb"

local t = setmetatable({}, meta)

print(table.concat(t, "", 1, 0x1 << 30))

Never do this in a production code.
You're relying on unspecified behavior here.
When evaluating an _expression_, Lua does not guarantee the order of subexpressions
(unless it is defined explicitly, e.g by operator precedence rules).
In particular, Lua does not guarantee the order of reading table elements inside table.concat.
Think what would happen with your code if table.concat read the table in reverse direction.