Is it possible in an array to get the array index? #3009
-
I'm testing with lua code, to obtain array using metatable but when I define my array, it does not return the index for example: local function CreateMtbl(tbl)
return {
__index = function(t, k)
return tbl.index[k](t.ptr)
end,
__newindex = function(t, k, v)
return tbl.newindex[k](t.ptr, v)
end,
__tostring = function(t)
return t.ptr
end
}
end
local function CallFunc(ptr, pointer, func)
local function idx(t, i)
return func(pointer(t.ptr, i))
end
local function tostring(t)
return t.ptr
end
return setmetatable({ptr = ptr}, {__index = idx, __tostring = tostring})
end
--- @class var_cast
--- @field id integer
--- @field name integer
--- @field timeout integer
--- @return var_cast
local var_cast = {
id = func.get_id,
name = func.get_name,
timeout = func.get_timeout
}
function MyVarCast(ptr)
return setmetatable({ptr = ptr}, CreateMetable(my_class))
end
local my_class = {
var4 = function (ptr) return CallFunc(ptr, func.get_var4, var_cast) end
}
--- @class my_class
--- @field var4 var_cast[]
--- @return my_class
function MyClass(ptr)
return setmetatable({ptr = ptr}, CreateMtbl(my_class))
end
When I do local myid = MyClass(myptr).var4[1].id he returns me What can I do to get it to return the index for example: for i = 0, 10 do
local m_id = myid.my_class[i].id
end expected return
local m_id = myid.my_class[1].id expected return
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I don't think this is ever possible. 😕 In LuaLS there is no dynamically created classes, all classes must be defined by
local myid = MyClass(myptr).var4[1].id
|
Beta Was this translation helpful? Give feedback.
-
Is there something that symbolizes then |
Beta Was this translation helpful? Give feedback.
I don't quite understand the question here. 😕
I can see what you want to achieve from your given examples above, but I don't think they are conceptually correct.
AFAIK, LuaLS aims to provide type inferences on variables and table fields.
By table fields it means fields which belong to a table/class, or in general a type container.
id
belongs to the typevar_cast
=> that's why it is showing(field) var_cast.id
var_cast.1
/var_cast.i
type which contains aid
field.This concept seems totally wrong to me 😕😕
Let's consider the following example, a hash table of
id: integer
=>Object