Yes, but the typechecking will assume it returns a Vector2 (since it’s the developer that says what type a function will return)
I see, if anything it makes it more readable, correct?
Which is nice
What if you were to return multiple types of variables? How would you do that with the syntax?
Can’t have multiple return types, you will have to do something like:
return {
1, 2, 3, 4, 5
}
That’s not true, you can do
return arg1, arg2, arg3
If you wanted multiple return types, you could use the “|” character:
function getPartOrModel(): BasePart|Model
local object = math.random(1,2) == 1 and Instance.new("Model") or Instance.new("Part")
return object
end
local x = getPartOrModel()
Yes you can do that too, but it’s more readable in a table for what I’m using it for
Even more interesting
hmmmmmmmmmmm
Thanks for this clarification, but what I meant originally was returning multiple variables, like this:
function foo()
return bar, wizz
end
I was talking about the function part, not the return keyword, but apparently you can just use |
Define it as a string then, since it will only return strings
Ah, I see. You can do this then:
function getPartAndModel(): (BasePart,Model)
return Instance.new("Part"),Instance.new("Model")
end
local x,y = getPartAndModel() -- x is assigned type BasePart, and y is assigned Model
If you need to return multiple values, use:
function foo(): (number, Vector2)
end
I haven’t seen it with LUA either. However, it looks like it might be a clue to Studio’s intellisense autocomplete as to what the function returns so when you reference it somewhere else, it can tell you what the return type is. Similar to adding markers to comments to describe the return values and parameters of functions/methods similar to this (C code):
// @param1@Parameter 1
// @param2@Parameter 2
// @return@Returns an integer value calculated form param1 and param2.
int addTwo(int param1, int param2)
{
return (param1 + param2)
}
The IDE intellisense will pick up on those comments and will show those items.
Yup, pretty much what I understand from it is what the type-checker expects it to return (you need a return function in order to use return types), it’s also more readable for the scripter