• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Is there a way to reffer a table itself inside the same table? (1 Viewer)

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
45
Reaction score
4
I searched and I didn't find anything, what I ask if I can do something like this, just to put an example:
Lua:
t={
    val=3
    ,EditVal=function()
        t.val=4
    end
}

print(t.val) --Should print 3
t.EditVal()
print(t.val) --Should print 4
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
75
Reaction score
46
Location
Italy
Website
github.com
No, you have to pass the table itself as argument to the function:

Lua:
t={
    val=3
    ,EditVal=function(t) --<------ 
        t.val=4
    end
}

print(t.val) --Should print 3
t.EditVal(t)
print(t.val) --Should print 4

-- You can also use the 'colon syntax' to execute the function:
t:EditVal() -- same as t.EditVal(t)
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
45
Reaction score
4
I searched about that and this is what I understand, the equivalent for what I want with colon syntax is:
Lua:
t={
    val=3
    ,EditVal=function(self)
        self.val=4
    end
}

print(t.val) --Should print 3
t:EditVal()
print(t.val) --Should print 4
Right?

Edit, now I have another question, if 2 tables have a method with the same name, Can't I use the colon syntax?
Lua:
t={
    val=3
    ,EditVal=function(self)
        self.val=4
    end
}

s={
    ,EditVal=function(self)
        self.val=4
    end
}

t:EditVal() --What does this?
Right?
 
Last edited:

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
75
Reaction score
46
Location
Italy
Website
github.com
As for the 'self', it's just the name of the function argument. Its scope is limited to the function. I called it 't', you called it 'self', but you can call it whatever you want (bar reserved keywords, of course). I personally don't like using 'self' that much because I feel that when there are too many 'self' in a script the code becomes unreadable.

Regarding the second question, x:f(a, b, c) is just synctatic sugar. It's exactly the same as writing x.f(x, a, b, c). Thus:

Lua:
t:EditVal( )
-- is the same as:
t.EditVal(t)

That is, it executes the EditVal member of t, passing t as argument (i.e. as 'self'). The end result is to assign 4 to t.val.
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
45
Reaction score
4
As for the 'self', it's just the name of the function argument. Its scope is limited to the function. I called it 't', you called it 'self', but you can call it whatever you want (bar reserved keywords, of course). I personally don't like using 'self' that much because I feel that when there are too many 'self' in a script the code becomes unreadable.

Regarding the second question, x:f(a, b, c) is just synctatic sugar. It's exactly the same as writing x.f(x, a, b, c). Thus:

Lua:
t:EditVal( )
-- is the same as:
t.EditVal(t)

That is, it executes the EditVal member of t, passing t as argument (i.e. as 'self'). The end result is to assign 4 to t.val.
Oh ok, I got it. PD: I think I should put somthing like s.EditVal(self) does set self.val=5 to make more understandable what I said, but I think you understood it anyway.
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
75
Reaction score
46
Location
Italy
Website
github.com
All right. Now, if my assumption is correct and you're heading to object oriented programming, the next step would be to introduce metatables.

That is, you can put the EditVal function in a table as 'mt' in the code below, and then set this table as the metatable of t, of s, and of any other similar 'object' you want to create. This way, all these objects will have the EditVal 'method'.

Lua:
mt = {  __index = { EditVal = function (x) x.val = 4 end } }

t = { val = 3 }
setmetatable(t, mt)

s = { val = 1 }
setmetatable(s, mt)

t:EditVal( ) -- this sets t.val = 4
s:EditVal( ) -- this sets s.val = 4
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
45
Reaction score
4
All right. Now, if my assumption is correct and you're heading to object oriented programming, the next step would be to introduce metatables.

That is, you can put the EditVal function in a table as 'mt' in the code below, and then set this table as the metatable of t, of s, and of any other similar 'object' you want to create. This way, all these objects will have the EditVal 'method'.

Lua:
mt = {  __index = { EditVal = function (x) x.val = 4 end } }

t = { val = 3 }
setmetatable(t, mt)

s = { val = 1 }
setmetatable(s, mt)

t:EditVal( ) -- this sets t.val = 4
s:EditVal( ) -- this sets s.val = 4
Oh nice, it will work better, in what section I can learn more of this?
Edit: I found it in the sections 13 and 16 of the reference manual.
 
Last edited:
Top