Difference between equals and colon equals?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CharlesMerriam

Is there any difference between these?
a = b
a := b
a := b;

The grammar shows semicolons, but never the colon equals.

Various examples don’t show semicolons but do show the colon equals.

Got it! := may be used in the line declaring a variable to set its type.

This works:

var a = 3
a = "hello"

This does not:

var a := 3 # a is now typed as int, same as var a:int = 3
a = "hello" # Parser Error: The assigned value's type (String) doesn't match the variable's type (int).

:bust_in_silhouette: Reply From: jgodfrey

a = b

Simply assigns the value of b to the variable a.

a := b

Also assigns the value of b to the variable a. In this case, the : is used to infer the data type of variable a based on the value of variable b. This is used in conjunction with Godot’s support for static typing. More details here:

a := b;

Really, this is the same as the previous example. Many languages use a semi-colon as an end of command indicator. In gdscript, the semi-colon is optional. It’s useful to 1) put a set of independent commands on a single line in a source file or 2) if you’re used to a language that requires the use of a semi-colon, and don’t want to be bothered to not use it when coding in gdscript

:bust_in_silhouette: Reply From: Millard

yes there is. GDscript is a dynamic language, meaning that variables don’t require you to declare the type of variable you are using, and the type can be changed at any time. However you can declare them if you want like this.

var: a: string = b

this declares the variable a as a string type, and sets it to be.

You can also let the compiler infer the variable type using :=

var a := b 

and the compiler will automatically infer that its a string type.

hope this helps!