Skip to content

Variable Declaration Statement

IsaacShelton edited this page Mar 21, 2022 · 1 revision

Variable Declaration Statement

Variables are usually declared using the variable declaration statement.

In order to declare one, you specify a name for the variable followed by it's type

my_variable Type

Initial Value

The newly declared variable can immediately be assigned a value by using = followed by a value

total_wins int = 21

Const variables

Variables can be made strictly-immutable by prefixing their declaration with const

const pi double = 3.14159265

Variables declared as const, cannot have their address taken or be assigned new values

Static variables

Variables can have their value persist between calls by declaring them as static

import basics

func main {
    repeat 3, print(getNextInteger())
}

func getNextInteger() int {
    static next int = 1
    return next++
}
1
2
3

Static variables that have a __defer__ method will have it called when the program exits.

Multiple variables

Multiple variables can be declared at one, by separating the variable names with commas

x, y, z float = 0.0f

Although multiple variables can be declared on the same line, they all must share an initial value

Disabling Zero-Initialization

By default, variables are zero-initialized. If this behavior is undesired, you can set the initial value to undef in order to leave the memory uninitialized

not_used_yet double = undef

POD Variables Declarations

Variables that are marked as POD (plain-old-data), are immune to __defer__ management procedures.

manual_string POD String = "Hello World"

See POD variables for more information.

Clone this wiki locally