Variables and Constants

Declare new variables using "const" or "var".

>>>> const x = 5

To read the value of a variable, use the variable name as an expression.

>>>> x
5: int

Scope of Variables

Welly has lexical scope, meaning that the x after return in the following function is the same x we've already declared.

>>>> fn get_x() { return x }
>>>> get_x()
5: int

Shadowing

Because we declared x to be const, we cannot change its value.

>>>> x = 7
ConstnessVMError: can only assign to "var" locations

However, we can declare a new variable called x.

>>>> const x = 444444
>>>> x
444444: int

The new variable shadows the old one. Any future use of the name x refers to the new variable. However, past uses of the name x still refer to the old variable.

>>>> get_x()
5: int

Mutable variables

Declare new variables using var if you want to be able to change their value. var variables mostly behave exactly the same as const variables.

>>>> var y = 6
>>>> y
6: int
>>>> fn get_y() { return y }
>>>> get_y()
6: int

The only difference is that you can assign new values to var variables.

>>>> y = 3
>>>> y
3: int
>>>> get_y()
3: int