Lua self references
Asked Answered
A

4

7

How exacyly do you get variables within a program with self?

Like in Java you have:

private int a

public void sa(int a) { this.a = a}
public void ga() { return this.a }

VB has 'ME' and C# has 'this' etc.

But whats the Lua equivalent of this? Is this in the right direction?

local a

function sa(a)
    self.a = a
end
Albertoalberts answered 22/6, 2013 at 22:21 Comment(3)
Yes, you answered your own questionLeeth
Really, wen I tried it didn't seem to workAlbertoalberts
yeah, that doesn't work. attempt to index a nil value (global 'self')Brandenbrandenburg
B
0

It is somewhat how you are saying it, but the OOP approach is a little bit different. The following is actually the method.

local t = {
    a
}
t.__index = t

function t:sa(x)
    self.a = x
end

And then, to call the function:

t:sa(21)

or

t.sa( t, "some string this time?" )
Beckmann answered 22/6, 2013 at 22:50 Comment(3)
Can you explain what __index does here exactly?Snips
@ThomasFarley __index here is merely implementing inheritance.Beckmann
there is no "true" OOP approach in Lua, as it does not define any OOP limitations, the only thing Lua provides in terms of remote OOP is the syntactic sugar of table:method(). The construct local t = { a } will create an empty table, unless a variable a is defined, in which case the value of variable a is written at index 1 to the table. And the __index key is useless by the way without setmetatable.Bridget
F
16

In lua, you dont have a specific class implementation but you can use a table to simulate it. To make things simpler, Lua gives you some "syntactic sugar":

To declare a class member you can use this full equivalent syntazes

  function table.member(self,p1,p2)
  end

or

  function table:member(p1,p2)
  end

or

  table.member = function(self,p1,p2)
  end

Now, comes the tricky part:

Invoking

table:member(1,2)

you get:

self=table,p1=1,p2=2

invoking

table.member(1,2)

you get:

self=1,p1=2,p2=nil

In other words, the : mimics a real class, while . resemble more to a static use. The nice thing is you can mix these 2 styles so, for example:

table.member(othertable,1,2)

gives

self=othertable,p1=1,p2=2

In this way you can "borrow" method from other classes implementing multiple inheritance

Forsterite answered 23/6, 2013 at 18:0 Comment(0)
R
4

Keep in mind that a:b(...) and function a:b(...) ... end is just a syntactic sugar. self does not necessarily point to the "current object" because unlike in other programming languages, self is just a variable and can be assigned to anything. See the example below for a demonstration:

function table:member(p1, p2)
 print(self, p1, p2)
end

is just

table.member = function(self, p1, p2)
 print(self, p1, p2)
end

and

table:member(1, 2)

is just

table.member(table, 1, 2)

hence

function table:member(self, p1, p2)
 print(self, p1, p2)
end

table:member(1,2) --self=1 p1=2 p2=nil

because that's just

table.member = function(self, self, p1, p2)
 print(self, p1, p2)
end

table.member(table, 1, 2) --self=1 p1=2 p2=nil
Reservist answered 8/4, 2019 at 19:37 Comment(0)
A
0

Apperantly it's exactly what I said.

local a

function sa(a)
    self.a = a
end
Albertoalberts answered 22/6, 2013 at 22:38 Comment(2)
local a is not needed; it doesn't have anything to do with the rest of the code.Arbela
No, it's totally different. 1) local a is useless, the a used inside function is the one received as parameter and hide the other one 2) self is a global AND has no value assigned, so it's nil 3) If you ada a line which calls sa, like sa("test") you will receive a run time error:"attempt to index global 'self' (a nil value)"Forsterite
B
0

It is somewhat how you are saying it, but the OOP approach is a little bit different. The following is actually the method.

local t = {
    a
}
t.__index = t

function t:sa(x)
    self.a = x
end

And then, to call the function:

t:sa(21)

or

t.sa( t, "some string this time?" )
Beckmann answered 22/6, 2013 at 22:50 Comment(3)
Can you explain what __index does here exactly?Snips
@ThomasFarley __index here is merely implementing inheritance.Beckmann
there is no "true" OOP approach in Lua, as it does not define any OOP limitations, the only thing Lua provides in terms of remote OOP is the syntactic sugar of table:method(). The construct local t = { a } will create an empty table, unless a variable a is defined, in which case the value of variable a is written at index 1 to the table. And the __index key is useless by the way without setmetatable.Bridget

© 2022 - 2024 — McMap. All rights reserved.