Did you know this about functions?

Did you know that when type-checking, you can add a question mark to make optional type-checking? Most programmers do it this way:

function PrintMessage(Message)
	print(Message or "Hello world!")
end

PrintMessage()

However, you can also do this as well, just in case if you like to use type-checking like I do:

function PrintMessage(Message: string?)
	print(Message or "Hello world!")
end

PrintMessage()

This can be useful in times where you want to add optional type-checking but can’t because of that ‘argument count mismatch’ warning. This might also be useful in other cases too.

Well, that was a rather short, but also informative tutorial. So, did you know that you could add a question mark to make optional type-checking? I hope you found this interesting, and I hope this was a useful topic. See you later!

7 Likes

Yes, It really isn’t an obscure Luau feature compared to Generics in my opinion, also most functions I see don’t use type checking, also most people usually do this for optional parameters:

Message = Message or "Hello World!"
3 Likes
1 Like