Closed UltraEngine closed 1 year ago
Can we get a similar example for implementing ipairs?
Pairs and ipairs probably are implemented the same way. Originally totally looked at the wrong things here.
The issue is that in your lua code you do this:
b = Table()
b[1] = 1
b[2] = 2
b[3] = 3
b[4] = 4
-- "Error: No matching function call takes this number of arguments and the specified types"
for k,v in ipairs(b) do
Print(b[n])
end
but n
in b[n]
is not defined.
If you use the same code for printing inside the for loop as you use in pairs then it should work.
Oh man, how did I miss that? It seems that sol uses size() to construct ipairs, so you don't even need a special iterator. This works as expected:
b = ctable()
b[1] = 1
b[2] = 2
b[3] = 3
b[4] = 4
b[5] = 5
b[4] = nil
b["key"] = "value"
b[7] = 7
for k,v in ipairs(b) do
Print(v)
end
I'm developing an STL-like table class for C++
https://github.com/UltraEngine/tableplusplus
Features
This is for user settings and other dynamic data that may be set in an application's C++ code or by a script extension.
All the following examples currently work.
Key / value in C++
Array in C++
Mixing Key / value and array-style keys
Key / value in Lua
Arrays in Lua
The only thing missing is support for ipairs:
I based my pairs() implementation off the code here: https://github.com/ThePhD/sol2/blob/7aae1aaaaa1bbcc03fd059fe38075cfe3f4b2e90/examples/source/pairs.cpp
Can we get a similar example for implementing ipairs?