I noticed it in the docs for for loops For,
So I looked back at what I’ve done so far and noticed that it’s also in the class declaration for the creative devices I’ve made. What does := do that = doesn’t? Thanks!
In Verse, :=
is the assignment operator, and =
is the equality operator used for comparison. This is unlike some other programming languages where the =
operator is used for assignment, and the ==
operator is used as the equality operator.
Coming from other languages, I feel likethis description is only partially correct.
The := operator is used to declare a symbol (e.g. a variable) with automatic/inferred type detection. Using set SomeVar = 1
for example is also assignment, but first SomeVar has to be declared with e.g. SomeVar : int = 1
- notice the type specifier int
. The int
type sepcifier can be omitted in some cases, like in a for loop as you already know - in that case, for (SomeInt := SomeIntArray)
is the same as for (SomeInt : int = SomeIntArray)
. In this case the for loop has declared the variable and can infer the type for you, since SomeIntArray has already been previously declared.
That is the correct description. I think it’s also important to point out that you can only infer (use :=) when declaring a constant (so not with a var) and you can only infer in a local context (not outside the class).
Also explained here: Constants and Variables