I’m trying to practice OOP and I got some fun ideas and hopefully useful ideas too so that I can write code faster and worry less in the future.
And one of the ideas is to be able to pass different values to the same fucntion and also get a different result! Similar to when you, for example, create a new CFrame value and it gives you the option to put nothing, put a Vector3 value, put 2 Vector3 values and so forth.
But the thing I’m trying to accomplish is… Well, I don’t know how to name it.
Here is what I’m talking about:
It’s the popup that you get which tells you what to pass to the function and a brief description of what it does. And if you don’t think you have enough control of the things you’re putting into the function, simply check the other 5 options!
So I’d really like to make this since it would make literally any code awesomer (if implemented ofc).
If it’s not possible then that’s a shame
Also if someone knows what this is called, please let me know
A surprisingly useful guide that I didn’t know I needed! And it definitely helped me in understanding typechecking.
But sadly it’s not what I’m trying to achieve in the post I made.
What I’m trying to make is like custom descriptions and visually show what values you can pass into the function.
For example, if I start typing Car.new(), it will show this:
And if want more control, then I can just click the right arrow in the top left and then it will show me this:
-- Server script or something idk
local Test = require(ReplicatedStorage.Test).new(Vector3.new(0, 0, 0))
Test:init()
-- Test class (in its own module script somewhere)
local Test = {}
Test.__index = Test
function Test:_print()
print(self.info)
end
function Test:_setInfo()
return `Returns a {typeof(self.value)}` -- can add extra information to the string if needed
end
function Test.new(value: Vector2 | Vector3)
local self = setmetatable({}, Test)
self.value= value
self.info = ""
return self
end
function Test:init()
self.info = self:_setInfo()
self:_print()
end
return Test
This is known as method overloading which typically exists in statically-typed languages specifically designed for OOP. Lua is a dynamically-typed* multi-paradigm language which does not have OOP built into the language itself. You can create an imitation of OOP using metatables but it’s not “real” OOP since an object in Lua is basically a fancy table, whereas in OOP languages, an object has its own representation in memory. A “method” is basically a function of a class. You could call it “function overloading” if you want.
The reason why Roblox data types like CFrame can have multiple functions with the same name is because these classes are implemented in C++ which allows for function overloading.
*Luau is an extension to Lua that allows for type-checking, but it still doesn’t allow function overloading.
But what WizulousThe2nd said, maybe it could be possible with plugins.
From what I know, plugins do have some fancy features just for scripts, but I don’t think it will be worth spending the time making this.
And thank you a lot for identifying that this is “function overloading”, because I couldn’t find these words when I was trying to search for this specific thing. I might just suck at explaining stuff to Google lol
You can create overloaded function types with the & type operator. This is purely editor-based and you’ll still have to add logic to handle different argument amounts.
type func1 = () -> number
type func2 = (arg1: number, arg2: number) -> number
local func = function(arg1, arg2)
if arg1 and arg2 then
-- do things with 2 args
return arg1 + arg2
else
-- do with none
return 1
end
end :: func1 & func2 -- typecast must be in this format
Oh what?! Didn’t know that it actually IS possible!
Honestly don’t know what to say other than a big thank you for this!
The most unexpected thing so far is actually seeing the “&” symbol used for this.
That still leaves the “how do I add descriptions for each function in that little popup when typing the function” question wide open, but honestly descriptions don’t really matter anymore for my situation.
Again, I don’t know what else to say except thank you!