When functions are defined in a module, what is the difference between these two types of definitions?

When functions are defined in a module, what is the difference between these two types of definitions:


Module1.lua

module(…, package.seeall)

function Module1:myFunction1()
–[[
various statements
–]]
end


Module2.lua

module(…, package.seeall)

function myFunction2()
–[[
various statements
–]]
end


[import]uid: 295 topic_id: 1341 reply_id: 301341[/import]

First one inserts an invisible “self” as first parameter which contains Module1 … Second one does not… [import]uid: 6928 topic_id: 1341 reply_id: 3629[/import]

In what cases would the first be desired over the second?
For my second example, is this definition legal? (notice that a period and not a colon is used between module name and function name):


Module2.lua

module(…, package.seeall)

function Module2.myFunction2()
–[[
various statements
–]]
end


[import]uid: 295 topic_id: 1341 reply_id: 3630[/import]

It all depends on what you wanna do. Lua is very “free” in that respect.

I would suggest you get a copy of “Programmingin Lua 2nd edition” and stick with an paradigm which matches your taste. [import]uid: 6928 topic_id: 1341 reply_id: 3636[/import]

I already got it and read it from cover to cover, twice. Still confused about this though [import]uid: 295 topic_id: 1341 reply_id: 3638[/import]

Your second example is valid syntax and identical to the second function definition in the first example.

Writing:

In the Module “modname.lua”

module(..., package.seeall)  
  
function modname.funcname() end  
  
-- is equivalent to  
  
function funcname() end  

as “module” is the table of the module itself and its functions are entries in the table
You can validate this by doing:

module(..., package.seeall)  
  
function funcname() end  
  
print(funcname)  
print(modname.funcname)  

which will show the identical value.

As will:

module(..., package.seeall)  
  
function modname.funcname() end  
  
print(funcname)  
print(modname.funcname)  

If you write it like this:

module(..., package.seeall)  
  
funcname = function() end  
  
modname.funcname = function() end  

You see that you actually access the “funcname” inside the table “modname”.

This is different to:

module(..., package.seeall)  
  
local funcname = function() end  

which will not add the “funcname” to the modname table.

The “modname” table is generated because of calling “module()”.
To come back to “:”

module(..., package.seeall)  
  
function funcname(self, x) print(self) end  
  
-- is identical to  
  
function modname.funcname(self,x) print(self) end  
  
-- is identical to  
  
function modname:funcname(x) print(self) end  
  

The colon is just “magic” for inserting the self as hidden parameter.

I hope that is answering your questions now!
[import]uid: 6928 topic_id: 1341 reply_id: 3641[/import]

Thanks a million. That pretty much clears it up except for one thing. In my 3rd example I used the period but didn’t use self:

module(…, package.seeall)

function Module2.myFunction2() end

Therefore (I understand everything. But…) in your last example, I have a question about line 7:

function modname.funcname(self,x) print(self) end

What happens if I done use 'self in the definition if ‘self’ isn’t needed in the body?:

function modname.funcname(x) print(x) end
[import]uid: 295 topic_id: 1341 reply_id: 3642[/import]

That was what I meant with it depending what you want to accomplish…

If you “just” have some functions in the module you would not use self at all. Like for example in an function collection with often used helper functions.

The self (and hence the colon) is for object oriented programming and has nothing to do with the module itself

I suggest read chapter 16 of PIL for that…

Basic rule: If you don’t know what self would be good for… you won’t need it :slight_smile: [import]uid: 6928 topic_id: 1341 reply_id: 3643[/import]

Dose that mean that the standard Corona statements like ‘text:setTextColor(255, 128, 128)’ are OOP method invocations that are invisibly passing ‘self’ to the setTextColor() method (function) and that setTextColor() is using ‘self’ internally?

local text = display.newText(“Hello!”, 20, 100, nil, 25)
text:setTextColor(255, 128, 128) [import]uid: 295 topic_id: 1341 reply_id: 3645[/import]

Yes of course…

display is the “helper” class.

It knows how to create a new display object of the type “Text” with the “newText()” method. This may be an “enhanced” version of the basic display object (as they all share stuff like remove/insert and other properties)… Dunno and it does not matter :slight_smile:

The object is returned and you assign it to text …

So if you use

text:setTextColor(255, 128, 128)  

It will send the text object as “self” into the setTextColor member function of the object which was created.

This does not necessarily mean that “display” has a member function of that name but the object it created for you has one.

To illustrate that “self” and the colon is just syntactical sugar you may use this

text.setTextColor(text, 255, 128, 128)  

… which works and is identical to the colon version.

Hope this answers your question :slight_smile: [import]uid: 6928 topic_id: 1341 reply_id: 3646[/import]

Got it! Thanks a million for your time. [import]uid: 295 topic_id: 1341 reply_id: 3647[/import]

You are welcome…

I can not just complain all the time :slight_smile:
[import]uid: 6928 topic_id: 1341 reply_id: 3650[/import]