有没有一种简单的方法可以在 Lua 中解压两个 arrays/varargs?

Is there an easy way to unpack two arrays/varargs in Lua?

让我们看一下这个伪代码示例:

-- Wraps a function with given parameters inside a callback
-- Usefull for sending class methods as callbacks.
-- E.g. callback(object.method, object)
local function callback(func, ...)
    return function(...)
         func(..., ...)
    end
end

我该怎么做? 我知道有 unpack,但它会被第二个 vararg 吞没:

local function callback(func, ...)
    local args = { ... }
    return function(...)
         func(table.unpack(args), ...)
    end
end

callback(print, "First", "Second")("Third") --> prints First Third

到目前为止我找到的唯一选择是将它们连接在一起然后解压:

local function callback(func, ...)
    local args1 = { ... }
    return function(...)
         local args2 = { ... }
         local args = { }
         for _, value in ipairs(args1) do
               table.insert(args, value)
         end

         for _, value in ipairs(args2) do
               table.insert(args, value)
         end

         func(table.unpack(args))
    end
end

这是唯一的解决方案,还是我可以做得更好? 我想要的是一个函数,它将两个数组连接在一起(应该比这两个 for 循环更快),然后在其上使用 table.unpack,或者使这些可变参数连接起来。

来自Lua 5.4 Reference Manual: 3.4 Expressions

If an expression is used as the last (or the only) element of a list of expressions, then no adjustment is made (unless the expression is enclosed in parentheses). In all other contexts, Lua adjusts the result list to one element, either discarding all values except the first one or adding a single nil if there are no values.

所以唯一的方法是在使用前手动将两个列表合并为一个列表。

有几种方法可以做到这一点。

for i,v in ipairs(list2) do
  table.insert(list1, v)
end

for i,v in ipairs(list2) do
  list1[#list1+i] = v
end

table.move(list2, 1, #list2, #list1 + 1, list1)

我不确定您在这里真正要解决的问题是什么。 如果你想从它的方法访问你的对象,请使用 self.

-- Wraps a function with given parameters inside a callback -- Usefull for sending class methods as callbacks. -- E.g. callback(object.method, object)

通常你会这样做:

local callback = function(params) object:method(params) end
callback(params)

而不是

callback(print, "First", "Second")("Third") 

你可以这样做

local callback = function (...) print("First", "Second", ...) end
callback("Third")

编辑(自以为是)

My main goal is to use member functions as callbacks. However, I'd like to keep it general. The advantage of a callback function is to omit the function and end keyword. It's shorter and looks closer to what it would look like, if there would be no need for a self argument. So this: object.event = function(...) return self:method(...) end would become to this: object.event = callback(self.method, self) what would be even nicer, is this (sadly not possible in lua) object.event = self:method

所以不用

RegisterCallback(function() obj:method() end)

你说这样比较容易,不用写function end?

local function callback(func, ...)
    local args1 = { ... }
    return function(...)
         local args2 = { ... }
         local args = { }
         for _, value in ipairs(args1) do
               table.insert(args, value)
         end

         for _, value in ipairs(args2) do
               table.insert(args, value)
         end

         func(table.unpack(args))
    end
end

RegisterCallback(callback(obj.method, obj)())

如果任何参数一开始都是 nil,您的方法就不会奏效。而且没有一个优势。您只是在输入其他单词,同时增加了 运行 出错的可能性。

您可以控制参数顺序来优化您的功能。

local function callback(func, ...)
    local args = {...}
    return function(...)
        func(..., table.unpack(args))
    end
end

callback(print, "second", "third")("first") -- prints first second third