Skip to content

Commit

Permalink
Merge pull request #311 from moonbitlang/fix-table
Browse files Browse the repository at this point in the history
feat: add bytes to zh_CN doc
  • Loading branch information
qazxcdswe123 authored Sep 30, 2024
2 parents 9495f3b + 9e0b267 commit 82e9cef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
26 changes: 12 additions & 14 deletions moonbit-docs/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn init {
```

#### Automatically insert `Some` when supplying optional arguments

It is quite often optional arguments have type `T?` with `None` as default value.
In this case, passing the argument explicitly requires wrapping a `Some`:

Expand All @@ -258,7 +259,7 @@ fn main {

Sometimes, it is also useful to pass a value of type `T?` directly,
for example when forwarding optional argument.
MoonBit provides a syntax `label?=value` for this, with `~label?` being an abbreviation of `label?=label`:
MoonBit provides a syntax `label?=value` for this, with `~label?` being an abbreviation of `label?=label`:

```moonbit
fn image(~width? : Int, ~height? : Int) -> Image { ... }
Expand All @@ -267,7 +268,6 @@ fn fixed_width_image(~height? : Int) -> Image {
}
```


### Autofill arguments

MoonBit supports filling specific types of arguments automatically at different call site, such as the source location of a function call.
Expand Down Expand Up @@ -328,7 +328,6 @@ Note that a conditional expression always returns a value in MoonBit, and the re
let initial = if size < 1 { 1 } else { size }
```


### While loop

In MoonBit, `while` loop can be used to execute a block of code repeatedly as long as a condition is true. The condition is evaluated before executing the block of code. The `while` loop is defined using the `while` keyword, followed by a condition and the loop body. The loop body is a sequence of statements. The loop body is executed as long as the condition is true.
Expand Down Expand Up @@ -519,9 +518,9 @@ fn main {

### Guard Statement

The `guard` statement is used to check a specified invariant.
If the condition of the invariant is satisfied, the program continues executing
the subsequent statements and returns. If the condition is not satisfied (i.e., false),
The `guard` statement is used to check a specified invariant.
If the condition of the invariant is satisfied, the program continues executing
the subsequent statements and returns. If the condition is not satisfied (i.e., false),
the code in the `else` block is executed and its evaluation result is returned (the subsequent statements are skipped).

```moonbit
Expand All @@ -530,9 +529,9 @@ guard index >= 0 && index < len else {
}
```

The `guard` statement also supports pattern matching: in the following example,
`getProcessedText` assumes that the input `path` points to resources that are all plain text,
and it uses the `guard` statement to ensure this invariant. Compared to using
The `guard` statement also supports pattern matching: in the following example,
`getProcessedText` assumes that the input `path` points to resources that are all plain text,
and it uses the `guard` statement to ensure this invariant. Compared to using
a `match` statement, the subsequent processing of `text` can have one less level of indentation.

```moonbit
Expand All @@ -543,25 +542,24 @@ enum Resource {
}
fn getProcessedText(resources : Map[String, Resource], path : String) -> String!Error {
guard let Some(PlainText(text)) = resources[path] else {
guard let Some(PlainText(text)) = resources[path] else {
None => fail!("\{path} not found")
Some(Folder(_)) => fail!("\{path} is a folder")
Some(JsonConfig(_)) => fail!("\{path} is a json config")
}
...
...
process(text)
}
```

When the `else` part is omitted, the program terminates if the condition specified
When the `else` part is omitted, the program terminates if the condition specified
in the `guard` statement is not true or cannot be matched.

```moonbit
guard condition // equivalent to `guard condition else { panic() }`
guard condition // equivalent to `guard condition else { panic() }`
guard let Some(x) = expr // equivalent to `guard let Some(x) = expr else { _ => panic() }`
```


## Iterator

An iterator is an object that traverse through a sequence while providing access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ fn main {
```

#### 在提供可选参数时让编译器自动插入 `Some`

许多可选参数的类型是 `T?`,默认值是 `None`。显式提供这种参数时,需要裹一层构造器 `Some`

```moonbit
Expand Down Expand Up @@ -545,24 +546,23 @@ enum Resource {
}
fn getProcessedText(resources : Map[String, Resource], path : String) -> String!Error {
guard let Some(PlainText(text)) = resources[path] else {
guard let Some(PlainText(text)) = resources[path] else {
None => fail!("\{path} not found")
Some(Folder(_)) => fail!("\{path} is a folder")
Some(JsonConfig(_)) => fail!("\{path} is a json config")
}
...
...
process(text)
}
```

当省略`else`的部分时,卫语句指定的条件不为真或者无法匹配时,程序终止。

```moonbit
guard condition // 相当于 guard condition else { panic() }
guard let Some(x) = expr // 相当于 guard let Some(x) = expr else { _ => panic() }
guard condition // 相当于 guard condition else { panic() }
guard let Some(x) = expr // 相当于 guard let Some(x) = expr else { _ => panic() }
```


## 迭代器

迭代器(Iterator)是一个用来遍历访问某个序列的元素的对象。传统面向对象语言(例如 Java),使用 `Iterator<T>``next()`
Expand Down Expand Up @@ -760,7 +760,7 @@ let zero = '\u{30}'
let zero = '\u0030'
```

### 字节
### 字节(序列)

在 MoonBit 中,字节字面量可以是一个 ASCII 字符或一个转义序列,它们被单引号`'`包围,并且前面有字符`b`。字节字面量的类型是 Byte。例如:

Expand All @@ -773,6 +773,16 @@ fn main {
}
```

`Bytes` 则是一个字节序列。类似于字节字面量,字节序列的字面量形式为 `b"..."`。例如:

```moonbit live
fn main {
let b1 : Bytes = b"abcd"
let b2 = b"\x61\x62\x63\x64"
println(b1 == b2) // true
}
```

### 元组

元组是一个有限值的有序集合,使用圆括号 `()` 构造,其中的元素由逗号 `,` 分隔。
Expand Down

0 comments on commit 82e9cef

Please sign in to comment.