Can you assign which self will be used?

Soo, i came up with this code:

	if self.__init then
		self.__init(object, ...)
	end
-------------------------------------------------------------------------------
function Module:__init(...)
	print(...)
end

I want to ask, why object which is table is not listed? can you assign selfs? i’m confused

Here is tutorial that i made code based on Efficient Object Oriented Programming Tutorial

Module:__init runs when you call the new function or basically when you create a new object

module:new()
   — Bla bla bla
   return obj 
end
module:__init(…)
   print(“Initialised with”,…)
end
local o = module:new(“a”,”b”)

So init would run when you create a new object.
As for self.__init(), if it exists then we are simply passing on the obj and any number of arguments to it

2 Likes

i asked if i can pass object for which self would be set

self.__init(object,...) -- object is first argument, but you don't include it inside class

Yes you can. Once you supply the obj to self.init, you can make use of it in the module init function
Example from GPT:


Module = {}
Module.__index = Module

function Module:new(…)
    local obj = setmetatable({}, Module)
    if self.__init then
        self.__init(obj,…)
    end
    return obj
end

function Module:__init(obj,…)
    print("Initialized:", obj,…)
end

-- Creating an instance
local instance = Module:new(3,6,”g$”)

2 Likes

hmm ok, but i want to ask if i don’t include object, will it auto-pass as new self? or it will not?

Apologies if I don’t quite understand your question, but do you want to know if calling .__init() on an instance of your object will automatically declare the self variable inside the __init function? Calling a function using the . prefix does not pass the object as the first parameter to your function. Using : to call your function will do so however. To clarify you can look at the example below:

local obj = {
    Text = "Hello, World!"
}

function obj.test(obj, ...)
    print(obj.Text) --Prints "Hello, World!"
end

obj.test(obj)
obj:test() --This is the exact same as obj.test(obj) just that lua automatically passes obj as the first parameter


--You can also directly create functions using the : prefix like this which automatically makes the first parameter self:
function obj:test2(...) 
    print(self.Text) --Prints "Hello, World!"
end

obj:test2()
obj.test2(obj) --These two function calls once again do the same thing as seen in the example above
```
1 Like

I want to understand this:

self.__init(object,...)

But inside module, we have this:

function Class:__init(...) -- there is no object, even if we called it
    print(...) -- prints variables, not object table
end

This is what i asked for, as i don't understand how is it possible that we pass object as first argument, but when we define first argument as ... it doesn't print it

Also we can replace … with variable list for example

function Class:__init(name, parent)
    print(name, parent) -- it will still print arguments passed after object
end

For me is that object is passed as self or smth, because it can’t magically vanish

The : function prefix automatically adds an extra parameter during creation of your function

function Class:__init(name, parent)
    print(name, parent) -- it will still print arguments passed after object
end

Is the same as:

function Class.__init(self, name, parent)
    print(name, parent) -- it will still print arguments passed after object
end
2 Likes

OK, I understood it after performing experiment, i found that if we have methood, but we will call it by dot, first argument that we will pass is self, if we have no argument when calling it there will be no self too

self.__init("test", object) -- in this example "test" will be self
self.__init(object, "test") -- in this object will be self
function Class:__init(name: string)
    print(self)
    -- 1. "test"
    -- 2. "{}"
end

Strange but usefull

2 Likes

You can also call functions using : by the way. In that case the parent table automatically gets passed as the first parameter.

function Class:__init(name: string)
    print(self) --self refers to the Class variable in this case 
    print(name) --Prints "Test"
end

Class:__init("Test")
--Is the same as:
Class.__init(Class, "Test")

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.