lua script - 가변 인자 variadic

sangwoo noh·2022년 12월 9일
0

lua script

목록 보기
1/1

루아 스크립트의 함수는 가변 인자를 받을 수 있다.

variadic

참고로 variadic의 뜻은 가변적인 variable 이라는 뜻과 - adic이라는 접미사가 합쳐져 생긴 단어다
즉, 여기서 variadic의 뜻은 개수가 변할 수 있는 인자를 뜻함.

-- variadi의 ref: https://en.wikipedia.org/wiki/Variadic
(javascript의 rest와 비슷함)

function rest(...)
    local sum = 0
    for i, v in ipairs {...} do
        print(i, v)
    end
    return {...}
end

local result = rest(2, "Greeting", {"hello", "hi"}, {
    color = "red"
})


print("-----------------------------")
print("key is [" .. 1 .. "]" .. ": " .. result[1])
print("key is [" .. 2 .. "]" .. ": " .. result[2])
print("key is [" .. 3 .. "]" .. ": " .. tostring(result[3]))
print("key is [" .. 3 .. "].hello" .. ": " .. tostring(result[3].hello))
print("key is [" .. 3 .. "][1]" .. ": " .. tostring(result[3][1]))
print("key is [" .. 3 .. "][2]" .. ": " .. tostring(result[3][2]))
print("key is [" .. 4 .. "]" .. ": " .. tostring(result[4]))
print("key is [" .. 4 .. "].color" .. ": " .. tostring(result[4].color))

--[[ // result
1 2
2 Greeting
3 table: 0x1599c20
4 table: 0x159d6c0


key is [1]: 2
key is [2]: Greeting
key is [3]: table: 0x1599c20
key is [3].hello: nil
key is [3][1]: hello
key is [3][2]: hi
key is [4]: table: 0x159d6c0
key is [4].color: red
]]

profile
하기로 했으면 하자

0개의 댓글