Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there ever a time when you should use method calls over function calls in your own code?

Asked by 5 years ago

Is there ever a time when you should use method calls over function calls in your own code? I can't think of any instances that require you to implicitly pass self because self isn't available. Any examples or explanations would be helpful.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Yes,specifically with object oriented programming, or OOP.


OOP


Object oriented programming , or OOP as it is abbreviated to, is an aspect of many languages that include classes such as java and c++

The primary use for self/methods in lua OOP is method inheritance, the being that methods are passed from a parent class to a child class, for example, in roblox, all instances have the properties, functions, and events of the instance class, being the parent class.


Example /w Code


Account = {balance = 0}

function Account:new (name)
  local o = {name=name}
  setmetatable(o, self)
  self.__index = self
  return o
end

function Account:deposit (v)
  self.balance = self.balance + v
  print(self)
end

local acc = Account:new("john")
acc:deposit(111)
print(acc.balance)

as you can see there, the acc subclass of the parent class Account inherits all methods and properties of said parent class, and any extra properties and methods you define.

Also, you may notice that when it is printing self in the deposit method when it was ran on line 16, it prints the acc table, or class

you can verify this yourself by doing:

print(acc)

the self in this case is always the table the method was executed from.

Now this also means you can make a subclass of a subclass, and then make a subclass of that, so on so forth.

However, if that isn't what is necessitated, the new function can be declared as a non-method, which is probably the reason roblox uses .new.

Hopefully this helped you understand the use of self better, and if you are any bit confused, just leave a comment


for the in depth explanation of OOP head chapter 16 of the lua pil here

0
Even in your example code, you could still do ``acc.deposit(acc, 111)``. I'm asking when you *need* to.  I can see using it at assignment time, but not with regular methods. little5 2 — 5y
0
then there is no case where you *need* to, you could put a non-implicit self parameter in every function everywhere, but that is considered bad practice and it is recommended that you declare a method whenever you have a need for the self parameter theking48989987 2147 — 5y
0
Also, your example is flawed. You're *trying* to use method calls in your code when they aren't necessary. I can rewrite your code to not use them and it works fine.  I'd like to see your source where it says it's bad practice not to use method calls. little5 2 — 5y
0
of course you can, you can rewrite every single method in every script ever to include a non-implicit self parameter, but why would you do that, its basically the equivalent of replacing every InputBegan with KeyDown theking48989987 2147 — 5y
View all comments (3 more)
0
So you're saying that non method calls are deprecated? I highly doubt that. little5 2 — 5y
0
what im saying that it is unnecessarily making code worse theking48989987 2147 — 5y
0
How is not using method calls worse? little5 2 — 5y
Ad

Answer this question