Variables

  1. name := 'Bob'
  2. age := 20
  3. large_number := i64(9999999999)
  4. println(name)
  5. println(age)
  6. println(large_number)

Variables are declared and initialized with :=. This is the only way to declare variables in V. This means that variables always have an initial value.

The variable’s type is inferred from the value on the right hand side. To choose a different type, use type conversion: the expression T(v) converts the value v to the type T.

Unlike most other languages, V only allows defining variables in functions. Global (module level) variables are not allowed. There’s no global state in V.

For consistency across different code bases, all variable and function names must use the snake_case style, as opposed to type names, which must use PascalCase.

Mutable variables

  1. mut age := 20
  2. println(age)
  3. age = 21
  4. println(age)

To change the value of the variable use =. In V, variables are immutable by default. To be able to change the value of the variable, you have to declare it with mut.

Try compiling the program above after removing mut from the first line.

Initialization vs assignment

Note the (important) difference between := and =. := is used for declaring and initializing, = is used for assigning.

  1. // failcompile
  2. fn main() {
  3. age = 21
  4. }

This code will not compile, because the variable age is not declared. All variables need to be declared in V.

  1. fn main() {
  2. age := 21
  3. }

The values of multiple variables can be changed in one line. In this way, their values can be swapped without an intermediary variable.

  1. mut a := 0
  2. mut b := 1
  3. println('$a, $b') // 0, 1
  4. a, b = b, a
  5. println('$a, $b') // 1, 0

Declaration errors

In development mode the compiler will warn you that you haven’t used the variable (you’ll get an “unused variable” warning). In production mode (enabled by passing the -prod flag to v – v -prod foo.v) it will not compile at all (like in Go).

  1. // failcompile nofmt
  2. fn main() {
  3. a := 10
  4. if true {
  5. a := 20 // error: redefinition of `a`
  6. }
  7. // warning: unused variable `a`
  8. }

Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name that is already used in a parent scope will cause a compilation error.

You can shadow imported modules though, as it is very useful in some situations:

  1. // ignore
  2. import ui
  3. import gg
  4. fn draw(ctx &gg.Context) {
  5. gg := ctx.parent.get_ui().gg
  6. gg.draw_rect(10, 10, 100, 50)
  7. }