Self parameter

Find quick help here to get you started with Hollywood
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Self parameter

Post by tolkien »

Hello friends.

In my learning curve in programming i´m trying to do some OOP. I understand concepts like objects, classes etc etc but I can´t understand the "self" concept used in objects.
I know it´s a generic question that i could try to resolve out there but I have tried Googleing and cant understand.

Thanks people!
jalih
Posts: 281
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Self parameter

Post by jalih »

Self parameter is just a variable that Hollywood automatically passes to a table's method and it is a reference to that table.

Instead of a self, you could directly use table's name instead.

Lets take a simple Point class for an example

Code: Select all

; Point "class"
Point = { x = 0, y = 0 }

; Methods for point "class"
Function Point:new(p)
	p = p Or {}
	SetMetaTable(p, self)
	self.__index = self
	Return(p)
EndFunction

	
Function Point:add(p)
	Local pp = Point:new( { x = self.x, y = self.y } )
	pp.x = pp.x + p.x
	pp.y = pp.y + p.y
	Return(pp)
EndFunction


Function Point:sub(p)
	Local pp = Point:new( { x = self.x, y = self.y } )
	pp.x = pp.x - p.x
	pp.y = pp.y - p.y
	Return(pp)
EndFunction


Function Point:mul(number)
	Local pp = Point:new( { x = self.x, y = self.y } )
	pp.x = pp.x * number
	pp.y = pp.y * number
	Return(pp)
EndFunction

	
Function Point:div(number)
	Local pp = Point:new( { x = self.x, y = self.y } )
	pp.x = pp.x / number
	pp.y = pp.y / number
	Return(pp)
EndFunction
	

Function Point:eq(p)
	If self.x = p.x And self.y = p.y Then Return(True) Else Return(False)
EndFunction
Interesting part is at the new method. Can you figure how it works? It gives default values and all Point table's methods available to a new table.

Example Usage:

Code: Select all

p0 = Point:new()                   ; Create zero point
p1 = Point:new( { x = 5, y = 5 })  ; Create Point p1
p2 = Point:new( { x = 2, y = 4 })  ; Create Point p2
p3 = p1:add(p2)                    ; Point p3 = p1 + p2
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

I need to read all again but thanks Jalih. You are always so helpful....
But, if you could directly use table's name why to use the self parameter?

Well, perhaps I have to learn more depthly the basics of oop.

Thanks!
jalih
Posts: 281
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Self parameter

Post by jalih »

tolkien wrote: But, if you could directly use table's name why to use the self parameter?
Usually in OOP you don't directly know the name of the owner object (=table) when writing methods and would need a way to pass that information to method. Hollywood and Lua do that automatically for you by supplying a self parameter to method.

Let's take my previous Point class for an example:

Code: Select all

p1 = Point:new()
It creates a new table p1 and copies what is inside Point table to it. If you would have used the Point table name inside the methods instead of a self parameter, then all the methods inside of the new p1 table would be referring to the original Point table. That would be bad... :D
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

Jalih, I think I understand the concept. Methods are general to all objects and when we create a new one we need to know to what who own who.

I´ll study the NEW method you write cos it´s a bit alien to me ;)

Thanks again Jalih, I´m going to practice.
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

After some reading I understand perfectly the concept of the Self parameter. With your explaining and the book LUA PROGRAMMING I think can experiment some OOP programming during a few weeks.

Thanks again, it´s fantastic to have people like you here.
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

More about OOP plz.

How can I simulate class constructors like in C++ in hollywood?
And...Its a good idea to use copytable to copy the class and make new objets?

I get no idea.
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Self parameter

Post by Allanon »

Well it's a good question because seems that around there are many way to implement oop under Lua (and Hollywood).
Using metatables to redirect instances methods calls to the parent class seems the most used implementation but to be honest after trying many solution I've choiced the simple way, so most of the time I prefer to make a copytable of the class to make my object independent. This way if at run time I change some things of the main class I'm sure that these changes will not be reflected into instantiated objects.
The bad using this implementation is that doing a copytable you copy methods too wasting memory for functions that could be declared only once in the root class. I've not tested, but I think that excluding the time needed to do the copytable the runtime should be a little faster because all methods are directly accessible in your objects.

I usually use the copytable with a function that override all object default values if the user pass them, just a little example:

Code: Select all

myClass = { x = 0, y = 0 }

Function myClass:New(parameters)
   Local NewObj = CopyTable(myClass)
   TB.Set(NewObj, parameters, False)
   Return(NewObj)
EndFunction
TB.Set(base, override, newtable) is part of Tables library (available here) and is used to override any values in the <base> table with the one specified in the <override> table, it's fully recursive. <newtable> is a flag that specify if it must create a new table or modify the <base> directly

Code: Select all

myClass = { x = 0, y = 0 }

Function myClass:New(parameters)
   Local NewObj = CopyTable(myClass)
   TB.Set(NewObj, parameters, False)
   Return(NewObj)
EndFunction

myInstance1 = myClass:New({x = 100})
myInstance2 = myInstance1:New({y = 20})
I think that depends of your projects and your coding style :)
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
jalih
Posts: 281
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Self parameter

Post by jalih »

Allanon wrote: I usually use the copytable with a function that override all object default values if the user pass them, just a little example:
Most of the time I prefer not using copytable and just create a table for class with a new method. In the new method, I create a local table for actual object and declare all methods directly for that local table and then return the table.

Metatable approach was used in my previous Point class example...
tolkien
Posts: 195
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

Very interesting!

Jalih, yo say more or less this?

Function new_star(x, y, color, dy)
Local object={x=x, y=y, color=color, dy=dy}
Return (object)
EndFunction

star1 = new_star(10,20, #red, 10)



Allanon, I have a question...

Why you use
Function myClass:New(parameters) for creating object instead of
Function myClass.New(parameters) ????

I understand the ":" if we use methods but I don´t understand it to create objects.

yes, I,m learning.
Post Reply