C++의 오랜 시간이 지난 후에 루아로 돌아갈 예정이며, 현재 좀 더 복잡한 것들에 대해 머리를 쓰려고합니다.루아 : 임의의 수의 반환 값
-- helper to call a function multiple times at once
function smartCall(func, ...)
-- the variadic arguments
local args = {...}
-- the table to save the return values
local ret = {}
-- iterate over the arguments
for i,v in ipairs(args) do
-- if it is a table, we unpack the table
if type(v) == "table" then
ret[i] = func(unpack(v))
else
-- else we call the function directly
ret[i] = func(v)
end
end
-- return the individual return values
return unpack(ret)
end
후 나는 같은 것을 할 수 있습니다 :
local a,b,c = smartCall(math.abs, -1, 2.0, -3.0)
local d,e,f = smartCall(math.min, {1.0, 0.3}, {-1.0, 2.3}, {0.5, 0.7})
인수의 임의의 수에 대한 함수를 여러 번 호출이 보이는 작은 유틸리티 기능을, 상상 이 작품은,하지만 내 버전은 언팩 및 임시 테이블을 많이 포함하기 때문에 더 convinient 방법이 있는지 궁금합니다. 당신이 C에서 smartCall
을 작성하는 경우
타이
작은 경고 : 코드는'func'가 단일 값을 반환한다고 가정합니다. 더 많이 반환하면 무시됩니다. 그게 니가 원하는거야? – kikito