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.1. Global declaration

A typical Wa-lang program source code is as follows:

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

import "errors"

const PI = 3.1415926

global 终极问题的答案: i32

func main {
    终极问题的答案 = getAnswer()
    println("宇宙的答案:", 终极问题的答案)
    println("π:", PI)

    err := errors.New("!!!")
    println("err:", err.Error())
}

func getAnswer => i32 {
    return 42
}

Similar to many languages, in Wa-lang, the part from the double slash // to the end of the line is a comment and has no actual effect.

Wa code consists of global declarations, like in the example above:

  • import "errors" is a declaration that imports the errors module
  • const PI = 3.1415926 is a constant declaration, which declares a constant named PI with the value 3.1415926
  • global The answer to the ultimate question: i32 is a global variable declaration, which declares a global variable named The answer to the ultimate question, of type 32-bit integer
  • func getAnswer => i32 {...} is a function declaration that declares a function that returns a 32-bit integer

There are 5 global declarations in Wa-lang. Each declaration starts with a specific keyword (followed by the entity of the declaration object). The relationship between the declaration and the corresponding keywords is as follows:

  • global: global variable declaration
  • func: function declaration
  • const: constant declaration
  • import: import declaration
  • type: type declaration

The following sections of this chapter will introduce global variable declarations, function declarations, constant declarations, and import declarations in sequence. Type declarations will be explained separately in Chapter 6.