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.4. Constant declaration

The constant declaration starts with the keyword const, and the general syntax is as follows:

const 常量名: 类型 = 常量值

For example:

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

const Answer: i32 = 42
const aConstString: string = "你好,凹语言"

func main {
    println(Answer)
    println(aConstString)
    println(aConstInt)
}

const aConstInt = 13

The output of this program is as follows:

42
你好,凹语言
13

When declaring a constant, if you do not specify a type (such as aConstInt in the above example), then it will be an untyped constant. There are 4 types of untyped constants: untyped integer, untyped floating point number, untyped characters, untyped strings, respectively:

const aUntypedInt = 11       // untyped integer
const aUntypedFloat = 13.0   // untyped floating point number
const aUntypedRune = 'a'     // untyped characters
const aUntypedString = "abc" // untyped strings

Arithmetic and logical operations on constant values are completed at compile time, such as:

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

const K = 4200000000000000000000000
const J = 4200000000000000000000000

func main {
    println(K/J)
}

Although the values of K and J both exceed the expression range of i64(the largest type in Wa-lang), the value of K/J can still be printed correctly. This also reflects that numeric constants have an expression range and precision that exceeds the basic type of variables.

The relevant rules when assigning constants to variables will be discussed in detail in Chapter 3.