Lua multiple assignment with tables

前端 未结 4 1914
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 08:36

This code:

function foo()
    return 1, 2, 3
end

bar = {}

bar = {a, b, c = foo()}

produces:

bar.a = nil
bar.b = nil
bar.c         


        
4条回答
  •  悲&欢浪女
    2021-01-12 09:09

    If you can, have foo() return a table formatted the right way.

    function foo()
        return {a = 1, b = 2, c = 3}
    end
    
    bar = foo()
    

提交回复
热议问题