Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

When functions are defined in a module, what is the difference between these two types of definitions?
Started by tokyodan Jul 02 2010 09:53 AM

- - - - -
11 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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

---------------------------------------------------
uid: 295 topic_id: 1341 reply_id: 301341


[TOPIC: post.html]
#2

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

First one inserts an invisible "self" as first parameter which contains Module1 ... Second one does not...
uid: 6928 topic_id: 1341 reply_id: 3629


[TOPIC: post.html]
#3

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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

---------------------------------------------------

uid: 295 topic_id: 1341 reply_id: 3630


[TOPIC: post.html]
#4

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

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.
uid: 6928 topic_id: 1341 reply_id: 3636


[TOPIC: post.html]
#5

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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


[TOPIC: post.html]
#6

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

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!
uid: 6928 topic_id: 1341 reply_id: 3641


[TOPIC: post.html]
#7

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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
uid: 295 topic_id: 1341 reply_id: 3642


[TOPIC: post.html]
#8

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

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 :)
uid: 6928 topic_id: 1341 reply_id: 3643


[TOPIC: post.html]
#9

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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)
uid: 295 topic_id: 1341 reply_id: 3645


[TOPIC: post.html]
#10

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

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 :)

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 :)
uid: 6928 topic_id: 1341 reply_id: 3646


[TOPIC: post.html]
#11

tokyodan

[GLOBAL: userInfoPane.html]
tokyodan
  • Contributor

  • 260 posts
  • Guests

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


[TOPIC: post.html]
#12

OderWat

[GLOBAL: userInfoPane.html]
OderWat
  • Contributor

  • 347 posts
  • Corona SDK

You are welcome...

I can not just complain all the time :)
uid: 6928 topic_id: 1341 reply_id: 3650



[topic_controls]
[/topic_controls]