What is the use of type when creating functions

So i was just messing with functions and methods, and I saw things like

RaycastResult Raycast(Vector3 origin, Vector3 direction, RaycastParams raycastParams)
Instance FindFirstChild(string name, bool recursive)

from what I understand, the one on the left, is the type, and on the right is the use or something. So I thought, what if I can do this, I tested stuff like

function myFunction(argument number)

end
function myFunction2(argument: number)

end

the myFunction2 does not seem to error, but when I change it like

function myFunction2(argument: type(number))

end
function myFunction2(argument: "number")

end
function myFunction2(argument: )

end

it errors, but the error message says

Syntax Error: Expected type, got 'blah blah'

it said expected type, and so I thought the type is the right one, and the variable im using is the left one. I got confused as when using methods, the type is on the left.

Correct me if I said something wrong since those are what I understand, anyways what is the use of type when creating functions?

I searched for things like this but the results is not what I wanted

I’m not sure exactly what you’re trying to mention. In order to specify that a parameter must be given a certain type, you do

variablename: variabletype

If you want it to have a type of string

variable: string

And that’s basically it

Those examples you found, I’m guessing they were on the roblox dev wiki? They probably did that to make it clearer to understand what each parameter does and what it expects you to give it.

The one on the right is the type, while the left is the parameter name.

function myFunction(argument: number)
    print(argument) --some number
end

You can also specify a return type of a function like so:

function myFunction(argument: number): string
    print(argument)
    return 'some string'
end

You’re not supposed to put the type in quotes or use the type() function. It must be exactly how it is above.

Finally, the use of types is to make sure the function receives the correct types so that nothing errors within. When you use strict mode (using --!strict signature), Luau lints (point out in red underline) type mismatches:

--!strirct
myFunction('wrong type') -- this is linted
myFunction(2) -- OK

local var: number = myFunction(2) --this is linted
local var: string = myFunction(2) -- OK

Otherwise in non-strict or no-check mode, currently, there’s not much of a “use” for them. They don’t error if you have type mismatches.

I strongly recommend reading Roblox’s guide on Luau type checking.

4 Likes

Ok thanks for your answers, I will mark as solution once I am satisfied with the answer

what is type-casting

what do you mean by using strict mode? how do I use it

I think I used the wrong word in this case. what I meant to say was that inorder to specify the type of parameter in a function, you do

variablename: number to specify that it’s a number and that you must give it a number

In order to use strict mode, on the top of your script, just write --!strict

2 Likes

In addition to what everyone has already said, you could look at one the recap announcements

1 Like

ok I think I understand now, I also tested the --!strict and it works

1 Like

That’s good! I recommend reading up on the recap @HugeCoolboy2007 posted to help give you a more detailed explanation on Type assertion (the thing you wanted help on). If you have anymoer issues don’t be afraid to make another post!

2 Likes

also thanks to @TheCarbyneUniverse and @HugeCoolboy2007 for your attempt to help

1 Like

@EmbatTheHybrid function is a type and a keyword, how can I make a function, expect a function for an argument, since if i just typed function alone, roblox thinks I am creating a function or something.

I’m not sure if that’s possible since I haven’t used Type checking before and am unsure if you could do that.

There’s a guide made by someone who commented about this same issue you’re having. If you have time, I would also recommend reading the guide too as it is quite helpful to help wrap Type Checking in your head

Here’s the response they made about it

But basically in short, since function is used as a keyword to create a function, I don’t think it’s going to be possible to check if something returns a function as of now

2 Likes

It is possible, here is an example

local function foo(bar: () -> string)
	return tonumber(bar())
end

print(foo(function()
	return "5"
end))
2 Likes

thanks, i though you need to type the word function itself

edit: I was just messing around with the function type argument, and I realised that you can make it expect to return a function that returns a function and yeah