hi guys, im trying to apply type notation into my code but i cant find any purpose in it other than just indicating things being a certain class or sort. i would like some examples of type notation in use and how i can learn a more practical usage of it if possible
its useful for me when i don’t know what is what, indicating what class something is apart of or what data type something is.
you can also export types via module scripts
Type notation is mostly used for public OOP modules, in case users are putting weird things as arguments when calling functions.
As for exemple, if the module function is supposed to be called with a bolean value as an argument and a user call it with a string or a number, it will return an error which will probably break the script, so using type notation prevent that.
It is also an additional check in case you have an argument that can be updated at anytime, like sometime it can be a string and the other time a Boolean, then the code in your function will probably not be the same depending of the type of the argument.
If you enable strict typing then it will be similar to typing in other programming languages like Java because what it does is prohibit wrong types.
Let’s say you have a function that must take in a number and returns a boolean. An example is isEven()
.
You might think that it’s very easy to implement so it’s hard to make a mistake. But, it’s just an example. Imagine a really long function or a function with a bunch of possible return results if you want to. This is for simplicity.
If it somehow returned nil
instead of boolean
which isn’t correct to you, then strict typing catches that and you find an error to fix it. If you accidentally didn’t give the function a number, then that gets catched too. The idea is to prevent you from headaches in debugging code. If you didn’t get these errors, you’d have to figure out yourself you wrote the wrong outputs or the wrong inputs. Have you ever gotten some bug but your code looks all fine? Our brains are biased to think we are right but errors help humble us.
When you said “indicating things being a certain class”, that is exactly why it’s good. You don’t make the mistake it’s a different certain class this way.
much appreciated, this explanation was solid. do you have any resources for using type notation with --!strict on? there are many situations i face where using type notation just overcomplicated everything with it on because there’s indication of errors for everything like an instance key not being able to find humanoid which makes no sense to me.
DevForum has a few community resources and there’s a website page here you can look at. Luau typing is probably not perfect so you could get a few weird errors (but it has vastly improved). There are a few Scripting Support questions that had them.
thank you man
afjaipjviolimitfapjip
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.