Lua function

Hi,

what is the difference between these two? Are different?

– file_1

local self = {}

self.login = function(_callback)

    – do something

end

retur self;

– file_2

local MyClass = {}

function MyClass:login(_callback)

– do something

end

return MyClass

Thanks!

Absolutely no difference between the two. (well return is misspelled in the first one).

However that said, in general “self” has a specific meaning.  It’s for a object to refer to itself.  I would almost consider it a reserved word.  For instance in the second variant:

local MyClass = {}

function MyClass:login(_callback)

      print(self.someProperty)

end

return MyClass

The assumption here is that you will have multiple versions of MyClass and each instance will have unique properties.  When a function is called using : instead of ., there is an implied variable called “self” that is set that is a reference to the actual object.

Rob

Both examples create a table at the top and return that table at the bottom of the file.

Both examples, you can name those tables anything you want.

* however I mostly avoid using ‘self’ as a table name, as self is often used as an implicit parameter in some cases. It just makes the code confusing sometimes.

So you could just as easily named those tables:

local M = {}

 … file code

return M

local joejoe = {}

… file code

return jojo

it is all the same.

The functions are different.

I use the second method always:

function MyClass:login(_callback)

– do something

end

 

I am not sure what advantage either ‘syntax’(style) has, I just use the second as it works, and keeps my code consistent.  I see a lot of other developers use the first method… maybe they can explain why, and what advantages it has. But the code within the function will still execute the same.

Thank you! Very clear

In the 2nd example (file 2),

It seem that can’t pass function like parameters:

Runtime:addEventListener(“enterFrame”, MyClass:login) does’t work

Why?

Here an example example:

local test = {} test.f1 = function(n)     print("f1:",n) end function test:f2(n)     print("f2:",n) end local function p(\_c)     \_c(3) end p(test.f1) -- Works! Output is: f1: 3 p(test.f2) -- Doesn't Works! Output is: f2: nil p(test:f2) -- Syntax Error

There’s a small difference between : notation and . notation.

When you use . notation, that object is just like any other table.

local myTable = {} myTable.number = 1 myTable.addToSelf = function( increment ) myTable.number = myTable.number + increment return myTable.number end return myTable

However, : notation has a special meaning. It’s a shorthand that keeps track of a reference to ‘self’, as Rob said.

local myTable = {} myTable.number = 1 function myTable:addToSelf( increment ) self.number = self.number + increment return self.number end return myTable

But technically, : notation is just a shorthand for doing this

local myTable = {} myTable.number = 1 myTable.addToSelf = function( self, increment ) -- do stuff end return myTable

There is always an implicit self as the first parameter when you use : notation. Lua won’t let you pass a reference to a function using : notation though, it’s specifically used for calling a function. That’s why you get the syntax error. You have to use . notation to pass references, BUT keep in mind the self parameter when you do.

If the function you are passing as a reference doesn’t take any parameters, then you can simply do this

local test = {} function test:f2() print("Hello") end local function p(\_c) \_c() end p(test.f2) -- Output is "Hello"

The reason why your test f2 didn’t work is because technically you are doing this

test.f2 = function(self, n) --self = 3 --n = nil print("f2: ", n) end local function p(\_c) \_c(3) end p(test.f2)

If you absolutely need to use : notation, then you have to wrap the call inside an anonymous function like this

local test = {} function test:f2(n) print("f2: ", n) end local function p(\_c) \_c(3) end p( function(n) test:f2(n) end ) -- Output is "f2: 3"

Absolutely no difference between the two. (well return is misspelled in the first one).

However that said, in general “self” has a specific meaning.  It’s for a object to refer to itself.  I would almost consider it a reserved word.  For instance in the second variant:

local MyClass = {}

function MyClass:login(_callback)

      print(self.someProperty)

end

return MyClass

The assumption here is that you will have multiple versions of MyClass and each instance will have unique properties.  When a function is called using : instead of ., there is an implied variable called “self” that is set that is a reference to the actual object.

Rob

Both examples create a table at the top and return that table at the bottom of the file.

Both examples, you can name those tables anything you want.

* however I mostly avoid using ‘self’ as a table name, as self is often used as an implicit parameter in some cases. It just makes the code confusing sometimes.

So you could just as easily named those tables:

local M = {}

 … file code

return M

local joejoe = {}

… file code

return jojo

it is all the same.

The functions are different.

I use the second method always:

function MyClass:login(_callback)

– do something

end

 

I am not sure what advantage either ‘syntax’(style) has, I just use the second as it works, and keeps my code consistent.  I see a lot of other developers use the first method… maybe they can explain why, and what advantages it has. But the code within the function will still execute the same.

Thank you! Very clear

In the 2nd example (file 2),

It seem that can’t pass function like parameters:

Runtime:addEventListener(“enterFrame”, MyClass:login) does’t work

Why?

Here an example example:

local test = {} test.f1 = function(n)     print("f1:",n) end function test:f2(n)     print("f2:",n) end local function p(\_c)     \_c(3) end p(test.f1) -- Works! Output is: f1: 3 p(test.f2) -- Doesn't Works! Output is: f2: nil p(test:f2) -- Syntax Error

There’s a small difference between : notation and . notation.

When you use . notation, that object is just like any other table.

local myTable = {} myTable.number = 1 myTable.addToSelf = function( increment ) myTable.number = myTable.number + increment return myTable.number end return myTable

However, : notation has a special meaning. It’s a shorthand that keeps track of a reference to ‘self’, as Rob said.

local myTable = {} myTable.number = 1 function myTable:addToSelf( increment ) self.number = self.number + increment return self.number end return myTable

But technically, : notation is just a shorthand for doing this

local myTable = {} myTable.number = 1 myTable.addToSelf = function( self, increment ) -- do stuff end return myTable

There is always an implicit self as the first parameter when you use : notation. Lua won’t let you pass a reference to a function using : notation though, it’s specifically used for calling a function. That’s why you get the syntax error. You have to use . notation to pass references, BUT keep in mind the self parameter when you do.

If the function you are passing as a reference doesn’t take any parameters, then you can simply do this

local test = {} function test:f2() print("Hello") end local function p(\_c) \_c() end p(test.f2) -- Output is "Hello"

The reason why your test f2 didn’t work is because technically you are doing this

test.f2 = function(self, n) --self = 3 --n = nil print("f2: ", n) end local function p(\_c) \_c(3) end p(test.f2)

If you absolutely need to use : notation, then you have to wrap the call inside an anonymous function like this

local test = {} function test:f2(n) print("f2: ", n) end local function p(\_c) \_c(3) end p( function(n) test:f2(n) end ) -- Output is "f2: 3"