The content on this page is written in Chinese, and then traslated into English by machine. More accurate traslations are welcome at: https://github.com/wa-lang/man/tree/master/en

Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."

2.2. Global variable declaration

Global variable declaration starts with the keyword global, and the general syntax is as follows:

global VarName: Type = initial value expression

For example:

// 版权 @2023 凹语言 作者。保留所有权利。

global aInt: i32 = 42     // 32-bit signed integer
global _num: f32 = 952.7  // 32-bit floating point number

func main {
    println(aInt)
    println(_num)
    println(名字)
    println(counter)
}

global 名字: string = "张三" // string
global counter: u32         // 32-bit unsigned integer

The output of this program is as follows:

42
952.7
张三
0

Global variables can be used anywhere inside the module - even if the declaration and use of global variables are in different source files, as long as they are in the same module; in the source file, there is no need to "declare first and then use". In the above example, the variables name and counter can reflect this feature.

It should be noted that the variable counter in the above example is declared without an initial value:

In Wa-lang, variables that are not given an initial value are always initialized with a value of 0, which helps eliminate uncertainty.