Variables

Declaring

A variable is declared with its name, its type and a starting value:

name: type = value
count: int = 0
price: float = 9.99
title: str = "Luma"
ready: bool = false
nums: [int] = [1, 2, 3]

Every variable must get a value when it is declared, so a variable never holds an undefined value. count: int on its own is a compile error.

A name can only be declared once:

x: int = 1
x: int = 2      // error: x is already declared at 1:1

A variable can’t be used before its declaration, not even in its own starting value:

x: int = x + 1  // error: undefined variable x

Assigning

To give an existing variable a new value, leave out the type:

count: int = 0
count = count + 1
count = count * 10
print(count)

Output:

10

The new value must have the variable’s type:

count: int = 0
count = "ten"   // error: cannot assign str to count, which has type int

Where a variable exists (scope)

A variable declared inside a block { } exists only until the block’s closing }:

if true {
    msg: str = "inside"
    print(msg)          // fine
}
print(msg)              // error: undefined variable msg (the one declared at 2:5
                        //        was inside a block that has ended)

Variables declared outside a block are visible inside it:

total: int = 10
if total > 5 {
    total = total + 1
}
print(total)

Output:

11

No shadowing

A block can’t declare a name that already exists outside it. That way one name always means one variable:

x: int = 1
if true {
    x: int = 2          // error: x is already declared at 1:1
}

Separate blocks can each use the same name, because the first variable is gone when its block ends:

if true {
    msg: str = "first"
    print(msg)
}
if true {
    msg: int = 2
    print(msg)
}

Output:

first
2

Lists, maps and sets are shared

Assigning a list, map or set doesn’t copy it: both variables refer to the same one. See Lists, Maps and Sets.

Locked values: lock

lock before a declaration makes a value that is fixed when the program is compiled and can never change:

lock PI: float = 3.14159
lock MAX_ITEMS: int = 100
lock APP: str = "Luma Notes"
lock TITLE: str = "${APP} (up to ${MAX_ITEMS} items)"

fn circle(r: float): float {
    return PI * r * r
}

print(TITLE)
print(circle(2.0))

Output:

Luma Notes (up to 100 items)
12.56636
  • The value must be known when compiling: literals, other locked values, and operators and interpolation on them. lock N: int = prompt() is a compile error.
  • Functions can use locked values. They can’t see a file’s variables (a variable might not have its value yet when a function runs), but a locked value is known before the program starts.
  • Only int, float, byte, bool and str can be locked. A list could still change inside, so it can’t be locked.
  • A locked value can’t be assigned to or freed, and its name can’t be declared again:
PI = 3.0              // error: PI is locked (at 1:6) and can't be changed
  • Only at the top level of a file, before it is used. pub lock shares it with the files that import this one (Imports).
  • Writing locked names in upper case is a habit, not a rule.

A locked value takes no memory: wherever it is used, the program has the value itself.