Skip to content

Commit

Permalink
doc: add fn main
Browse files Browse the repository at this point in the history
Closes #227
  • Loading branch information
peter-jerry-ye committed Jul 8, 2024
1 parent bb51b47 commit 1bdf378
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ When MoonBit reaches beta, it means any backwards-incompatible changes will be s

## Overview

A MoonBit program consists of type definitions, function definitions, and variable bindings. The entry point of every package is a special `init` function. The `init` function is special in two aspects:
A MoonBit program consists of type definitions, function definitions, and variable bindings.

### Program entrance

There is a specialized function called `init` function. The `init` function is special in two aspects:

1. There can be multiple `init` functions in the same package.
2. An `init` function can't be explicitly called or referred to by other functions. Instead, all `init` functions will be implicitly called when initializing a package. Therefore, `init` functions should only consist of statements.
Expand All @@ -34,6 +38,16 @@ fn init {
}
```

For WebAssembly backend, it means that it will be executed **before** the instance is available, meaning that the FFIs that relies on the instance's exportations can not be used at this stage;
for JavaScript backend, it means that it will be executed during the importation stage.

There is another specialized function called `main` function. The `main` function is the main entrance of the program, and it will be executed after the initialization stage.
Only packages that are `main` packages can define such `main` function. Check out [build system tutorial](./build-system-tutorial.md) for detail.

The two functions above need to drop the parameter list and the return type.

### Expressions and Statements

MoonBit distinguishes between statements and expressions. In a function body, only the last clause should be an expression, which serves as a return value. For example:

```moonbit live
Expand All @@ -54,8 +68,6 @@ fn init {
}
```

### Expressions and Statements

Expressions include:

- Value literals (e.g. Boolean values, numbers, characters, strings, arrays, tuples, structs)
Expand Down
15 changes: 12 additions & 3 deletions zh-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ MoonBit 目前处于 Pre-alpha 阶段,是实验性质的。我们期望今年
## 概述

一个月兔程序由类型定义,函数定义和变量绑定组成。
每个包的入口点是一个特殊的 `init` 函数,它有以下两个特点:

### 程序入口

有一个特殊的函数: `init` 函数,它有以下两个特点:

1. 同一个包中可以有多个 `init` 函数。
2. `init` 函数不能被显式调用或被其他函数引用。相反,在一个包初始化时,所有的 `init` 函数都将被隐式地调用。因此,`init` 函数中只能包含语句。
Expand All @@ -34,6 +37,14 @@ fn init {
}
```

对于WebAssembly后端而言,这意味着它将会在实例准备好**之前**被执行,也就是说,如果有FFI依赖实例的导出,那么将不能正常运行;对于JavaScript后端而言,这意味着它将会在被导入的时候执行。

另一个特殊的函数是`main`函数。`main`函数是程序的入口,它将会在初始化阶段之后被执行。只有是`main`的包中才能定义`main`函数。查看[构建系统教程](./build-system-tutorial.md)了解更多。

上述两个函数均需省略参数列表与返回值定义。

### 表达式和语句

MoonBit 区分语句和表达式。在一个函数体中,只有最后一句才能写成作为返回值的表达式。例如:

```moonbit live
Expand All @@ -54,8 +65,6 @@ fn init {
}
```

### 表达式和语句

表达式包括:

- 值字面量(例如布尔值、数字、字符、字符串、数组、元组、结构体)
Expand Down

0 comments on commit 1bdf378

Please sign in to comment.