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."
3.1. Local variable declaration
Chapter 2 introduces global variable and constant declarations. Also commonly used is local variable (variables defined within a function) declaration. Its general syntax is:
VarName: Type = initial Value
What is different from all variables and constants is that the declaration of local variables does not start with a keyword; when declaring a local variable, if the = initial value
part is omitted, the variable will be initialized with a value of 0, such as:
aI32: i32 = 42
aString: string = "你好"
aF32: f32 // 0.0
Another commonly used syntax for declaring local variables uses the short-declaration :=
. The syntax is as follows:
VarName := expression
When using this writing method, the type of the variable will be consistent with the type of the expression on the right side of the short-declaration, and the value of the expression will be assigned to the initial value of the local variable, such as:
a := 13 // int
f := 3.14 // f64
s := genString() // string
...
func genString() => string { return "Hello" }
Wa-lang is a statically typed language. The type of a legal expressions can be inferred at compile time, so the type of a expression is determined. The
:=
syntax is similar to C++'sauto
type.