Posted 18 June 2016 - 09:25 AM
When trying to unpack a table containing nil values using unpack I realised that this doesn't work as it will stop once the first nil value has been encountered, not dissimilar to ipairs.
Inline with the Lua documentation, I created my own implementation that handles nil values just how I need it to with the catch that the tables length must be supplied (n):
The issue is I am encountering stack overflows, unlike the built in `table.unpack`.
If I unpack `{nil, nil, "value"}` I get `{}`, with my custom unpack I get `{[3] = "value"}`. My goal is to create a function that does this without overflowing the stack when too many values are supplied.
For context: I need to be able to unpack nil values because I am using an argument resolution system that loops through each given argument (vararg).
The function is configured to take arguments x, y, and z. If I only want to define 'z', I can call it with `nil, nil, "value"` in normal Lua. I need this to be possible from a table as I am parsing the arguments through XML.
Inline with the Lua documentation, I created my own implementation that handles nil values just how I need it to with the catch that the tables length must be supplied (n):
function unpack (t, i, n)
i = i or 1
if i > n then return end
return t[i], unpack(t, i + 1, n) --# stack overflow occurs here
end
The issue is I am encountering stack overflows, unlike the built in `table.unpack`.
If I unpack `{nil, nil, "value"}` I get `{}`, with my custom unpack I get `{[3] = "value"}`. My goal is to create a function that does this without overflowing the stack when too many values are supplied.
For context: I need to be able to unpack nil values because I am using an argument resolution system that loops through each given argument (vararg).
The function is configured to take arguments x, y, and z. If I only want to define 'z', I can call it with `nil, nil, "value"` in normal Lua. I need this to be possible from a table as I am parsing the arguments through XML.