What does a : mean (in a function)

One of my friends learning about OOP wanted me to ask this because we are both curious. My theory is its interchangeable with “,” but i’m probably wrong.

What does the " : " in a function mean?
I’ve seen in OOP that people use a colon in functions to define stuff, but I don’t know what it means. If anyone knows, please let me know? An example of what I mean is ‘exampleFunction(var1 : thing)’

1 Like

using the colon just types the variable, meaning that it is easier to see what things you are passing to a function etc. Here is the luau typecheck article: Type checking - Luau

edit: Here is another section specifically for basic types: Syntax - Luau

2 Likes

If you do this:

local function GetPartCFrame(part: BasePart): CFrame
    return part.CFrame
end

When you call this function, the autocomplete will tell you that the first argument should be a BasePart. This is especially useful when using functions from ModuleScripts. Inside a function, you can use any member (property, method, event) of argument with the help of autocomplete. For example, in the function above, if you write part.To the autocomplete will suggest you event called Touched, as it knows that it is a BasePart. You may notice that I’ve put : CFrame after the function name. This means that the function returns a CFrame datatype. So if I need to get the CFrame of a part with this function (yes, you can just type local cframe = part.CFrame, but I couldn’t think of a better example), I’ll do it like this:
local cframe = GetPartCFrame(part)
And the autocomplete will know that cframe is a CFrame. So if I type cframe.Pos, the autocomplete will suggest a property called Position.

This can also be used with variables, not only with functions:

local variable: number = worskpace.Part.Size.X

This is just basics, you can read more in the article linked above.

8 Likes

Just to declare, you dont really have to use them, they are not necessary.

However, I think it’s can be good practice to use them.[As others wrote above].

1 Like

The other replies are for typechecking with Luau’s type system. As for your actual question, I’ll answer that for you.

In OOP there’s two basic types of functions for “Objects”:

Methods [:]

  • Meant for interaction with the Object directly, via the use of the self parameter.
  • Allows for interaction with the Object’s variables.
    Ex: GetChildren() is a Method for Roblox Instances, it directly uses the Children of the Object.
    Do note that Roblox Instances all use Method operations.

Functions [.]

  • Meant for general functionality that doesn’t directly use anything from the Object.
    Ex: string.format() is a Function of the string Object (Library) and doesn’t directly use anything from the string Object. You pass in a string and the formatter, and the Tuple of format parameters.

Summary: Methods with a colon have self, functions with a period don’t have self (unless passed in)

Edit: I’d also like to note, Methods can still be used with a period by simply passing in their Object as the first parameter. Ex: workspace.GetChildren(workspace)

2 Likes

I’ve also thought that @LucidPolygon is talking about this, but he is talking about type declarations using :.

My bad I didn’t notice that, but in that case then yeah, very useful for development. I recommend using it by all means, since random type errors are a headache when making a game.

1 Like

From the Lua Manual: Lua 5.1 Reference Manual

2 Likes