Implement optional type checking?

Hello! I am making a module that checks types for every function, this is to let the scripter know if they are making a mistake instead of just throwing an error line. However, I have noticed that this might not be the best for people that prefer performance and so it hit me. Is there a way I can include optional type checking? I am talking about a variable that if enabled will stop all type checks. The reason I am asking is that I want to know if it is performant and if it is even worth it. After all, if you do not want type checking then that means you might be very strict with loading times and as such I wonder if it is even worth to do so.

So should I do that? Should I add an if statement for every type check to improve loading times, or does the simple fact I am checking with an if statement makes it unnecessary?

3 Likes

luau has type checking built in

you can read more about it here:

so as you can see here there is a red line under "69" to indicate to the scripter that they are using the wrong type
image

also when typing the function it shows in a popup that it needs to be a number
image

2 Likes

What do you mean by “type”. You mean like built-in types (i.e. string, thread, Vector3, RBXScriptConnection)?

1 Like

There is already a module that does this in my knowledge. I believe its named t and yes I know, it sounds weird but believe me its a thing. And as @5uphi mentioned luau does have its own typechecker, which you can force by commenting --!strict at the top of the script/module, but t provides more flexibilty.

Here is the module if you wanna check it out.

Also hey suphi kaner in the chat, love ur vids man.

1 Like

I already tried this. However, since my module uses functions that return values it doesn’t work.

module:Require("Nice").hello(true)

Require returns a module and hello is a function inside that module. The default type checking would never reach that far

Like:

local myPart = arguments[1];
assert(typeof(myPart) == "Instance" and myPart:IsA("BasePart"), "Error at ... Expected part");

But wouldn’t that already nullify me trying to stop this behavior? I haven’t checked in depth but I believe this module does what I already did in the sense that they call type:

assert(type(hello) == "string", "Expected string");

That should in fact take less that the module since I am not indexing or calling separately. Remember that I am doing this for speed and sadly I think this won’t make it faster.

Well tbh then I rly dont c a point in implementing what u want bcuz its alr done by a typechecker. And as for your statement of default typechecking not working with a larger scope, your quite wrong there bcuz it does work, if thr typings are already set in tht module’s function.

It still calls type every time. That is what I mean. And I mean that is a fair point so I guess I won’t bother for now. But yes it does call type every time you want to check it.

So you want players to stop typehecking when they want to? And may I know whether u intend this for one script/module or globally?

Im sry with the questions but without any code for reference, its quite hard for me to understand the concept.

1 Like

Every function I do has type checking in the sense that it will tell the user if the variable they sent is invalid.

function(arg1, arg2)
	assert(type(arg1) == "string", "Error message here")
end;

I heard from somewhere that using type causes inefficiency and when I saw I was using so many type calls I decided it could be good if these were toggable. (If you know what you are doing then there is no need to type check and that will improve performance). However, I do not know if it is even worth it at all since it might not be such a big difference to do this:

function(arg1, arg2)
	assert(type(arg1) == "string", "Error message here");
	assert(type(arg2) == "table", "Error message here");
end;

to this:

function(arg1, arg2)
	if(typecheck) then
		assert(type(arg1) == "string", "Error message here");
		assert(type(arg2) == "table", "Error message here");
	end;
end;

No I don’t think typing causes any perfofmance issues, so u can very well go ahead and implement typechecking in your functions and just an FYI, top libraries like Knit does the exactly same thing that your doing and there is no performance issues with it. I will update my reply if I find other drawbacks but as of now, i believe there is none.

1 Like