# 初识者的若干反馈

**URL:** <https://taolun.moonbitlang.com/t/topic/1397>\
**Category:** MoonBit反馈\
**Tags:** moonbit-lang\
**Created:** [2026 年3 月 10 日 02:19 UTC](https://taolun.moonbitlang.com/t/topic/1397 "2026-03-10T02:19:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

作者： ![YDX](https://taolun.moonbitlang.com/user_avatar/taolun.moonbitlang.com/ydx/32/860_2.png) [@YDX](https://taolun.moonbitlang.com/u/YDX)\
发布日期： [2026 年3 月 10 日 02:19 UTC](https://taolun.moonbitlang.com/t/topic/1397/1 "2026-03-10T02:19:37Z")

</div>

大家好，拜个晚晚年了！

我为了审阅[某个PR]`(https://github.com/typst-community/wasm-minimal-protocol/pull/65)`，初步尝试了下MoonBit，不过遇到了很多困难。大部分困难我不确定是否算MoonBit的缺陷，算的话也不确定应该报到哪个 GitHub 仓库，所以就直接在这里反馈了。

> 注：“新用户在一个帖子中仅能发布 2 个链接”，我就变通处理了。

## `$MOONBIT_INSTALL_VERSION`及其格式不明

[安装 MoonBit CLI Tools]`(https://www.moonbitlang.com/download/#moonbit-cli-tools)` 界面没有介绍如何安装特定版本（而不是最新版）。然而对于时刻在变化的新语言，固定版本通常很必要，特别是在CI上。

我查看[`install/unix.sh`]`(https://cli.moonbitlang.cn/install/unix.sh)`，发现支持环境变量`$MOONBIT_INSTALL_VERSION`，但没找到是变量应该是什么格式。经过一番调查试验，最终确定它指定的是moonc的版本（而非moon），而且格式需要加hash。例如以下这个版本，应该要指定`MOONBIT_INSTALL_VERSION=0.8.2+8cca5f22a`。

```sh
$ moon version --all
moon 0.1.20260209 (b129ae2 2026-02-09) ~/.moon/bin/moon
moonc v0.8.2+8cca5f22a (2026-02-25) ~/.moon/bin/moonc
moonrun 0.1.20260209 (b129ae2 2026-02-09) ~/.moon/bin/moonrun
moon-pilot 0.0.1-df92511 (2026-02-25) ~/.moon/bin/moon-pilot

```

## 文档版本号可能过时

[MoonBit Documentation]`(https://docs.moonbitlang.com/en/latest/index.html)`选择 latest 时，`<title>`是 MoonBit v0.7.1 documentation，但根据上一节我操作的结果，目前 latest 好像是 0.8.2。这是文档落后呢，还是仅仅`<title>`标错了？

## `moon fmt`添加的`///|`缺少解释

对于没有注释的函数，运行`moon fmt`后会添加`///|`，如下。

```diff
+ ///|
  pub fn hi() -> Int {
    0
  }

```

我不清楚它和`///`有什么区别。我在 [MoonBit Language — MoonBit v0.7.1 documentation]`(https://docs.moonbitlang.com/en/latest/language/index.html)` 没有找到任何关于`///|`的解释。它第一次出现是在[介绍 async](https://docs.moonbitlang.com/en/latest/language/fundamentals.html#optional-arguments:~:text=For%20async%20functions%2C%20optional%20argument%20expressions%20can%20call%20async%20functions%20as%20usual:) 时，难道`///|`和 async 有关？

我询问通用LLM，回复说这是避免 markdown 列表被错误合并成一行，但我也没在文档中找到 line wrap 相关信息，而且多行都用`///|`的话，会被`moon fmt`修改：

```diff
  ///| - a
- ///| - b
+ ///|
+ /// - b
  pub fn hi() -> Int {
    0
  }

```

至今我仍不清楚`///|`的意义。

## `moon fmt`转换出的`moon.pkg`不稳定

如果用`moon.pkg.json`，运行`moon fmt`会转换为`moon.pkg`（这让我比较惊讶，但可能设计如此，也还好）。

问题在于，第一次运行时`moon.pkg`最后没有空行，第二次运行会给加上，正常应该一步到位。

## `Bytes`与`FixedArray[Byte]`等的关系不明

我在 [Bitstring Pattern — Fundamentals — MoonBit documentation]`(https://docs.moonbitlang.com/en/latest/language/fundamentals.html#bitstring-pattern)` 看到 MoonBit 有多种方式表示字节序列：

> `BytesView`, `Bytes`, `Array[Byte]`, `FixedArray[Byte]`, `ReadOnlyArray[Byte]`, and `ArrayView[Byte]`

其它几个还相对好理解，但`Bytes`和`FixedArray[Byte]`是什么关系呢？通用LLM说前者是后者的别名，但我实测好像二者不能互换，而且内存结构也不同（前者开头有八个字节的零，后者开头有四个字节0xFF和另外四个我不清楚意义的字节）。

此外，如果我定义一个函数，需要读取一串长度任意的字节而无需修改，那么函数的参数应该用哪种？这在文档里似乎没有解释。

我在论坛上看到[下面这段解释](https://taolun.moonbitlang.com/t/topic/1107)，但还是很模糊……

> [@请教一下关于Bytes](https://taolun.moonbitlang.com/t/topic/1107/2):
>
> Bytes在layout上和FixedArray[Byte]是等价的，都相当于c里的`byte[]`。但之后Bytes我们会调整为immutable的。
> 
> 从性能优化的角度来讲，Bytes的内存layout相较于FixedArray[Int]来说更紧凑一些，locality更好，但是FixedArray[Int]是native aligned的，访存延迟上可能会有一定的优势。

[DeepWiki 的分析]`(https://deepwiki.com/search/bytesfixedarraybyte_90421b28-fe57-4814-a7c4-27b160960db2?mode=fast)`也有一定帮助，但仍未解决问题。

## blit 函数系列不全

处理字节时，经常需要整块memcpy。根据我的调查，MoonBit把这类函数叫`blit_to`/`blit_from_*`。

然而MoonBit有多种方式表示字节序列，并不是每两种方式之间都能blit。我在 [core – mooncakes]`(https://mooncakes.io/docs/moonbitlang/core/)` 只查到了下面几种：

```moonbit
Array::blit_to(..., dst : Array[A], ...)
ArrayView::blit_to(..., dst : Array[A], ...)
FixedArray::blit_from_bytes(..., src : Bytes, ...)
FixedArray::blit_from_bytesview(..., src : BytesView)
FixedArray::blit_from_string(..., str : String, ...)

```

## FFI缺少字节

在 [Foreign Function Interface (FFI)]`(https://docs.moonbitlang.com/en/latest/language/ffi.html#types)` 中，Wasm后端并无任何字节、数组相关的类型，那应该怎样向host传递字节呢？

我们现在的做法是在`moon.pkg`设置`"export-memory-name": "memory"`，然后通过以下函数把 Bytes 的地址转成`Int`传出去。

```moonbit
#borrow(bs)
extern "wasm" fn data_ptr(bs : Bytes) -> Int =
  #|(func (param i32) (result i32)
  #| local.get 0
  #| i32.const 8
  #| i32.add)
// 以上加八是试验出来的，我没在文档中找到能确认的信息。

```

> Types not mentioned above do not have a stable ABI, so your code should not depend on their representations.

以上 FFI 文档说`Bytes`的 API 不稳定，那这种做法恐怕过几个版本就可能失效了？有没有更可靠的办法呢？

## E4042 解释不实

错误代码 [E4042 — MoonBit documentation]`(https://docs.moonbitlang.com/en/latest/language/error_codes/E4042.html)` 说：

> For example, you cannot use `FixedArray[T]` and `String` in FFI definition on WASM linear backend.

但我实测`FixedArray[Byte]`可以，而`FixedArray[Int]`确实不行。

## discuss 和 taolun 论坛的关系不明

我查到有 `https://discuss.moonbitlang.com/` 和 `https://taolun.moonbitlang.com/` 两个论坛。前者主要英文，后者主要中文，而且两论坛似乎相互均未提及。这两个论坛有取代继承关系吗？若是共同运行，建议置顶帖子介绍一下。

---

<div class="post-metadata">

作者： ![tonyfettes](https://taolun.moonbitlang.com/user_avatar/taolun.moonbitlang.com/tonyfettes/32/150_2.png) [@tonyfettes](https://taolun.moonbitlang.com/u/tonyfettes)\
发布日期： [2026 年3 月 11 日 09:02 UTC](https://taolun.moonbitlang.com/t/topic/1397/2 "2026-03-11T09:02:00Z")

</div>

感觉反馈。关于你的问题：

* * *

> 其它几个还相对好理解，但`Bytes` 和`FixedArray[Byte]` 是什么关系呢？

`Bytes` 和 `FixedArray[Byte]` 在内存结构上是一样的。你看到的不一致行为实际上是 `Bytes` 和 `FixedArray[Byte]` 的对象头（Object header）。对象头的存在也能够解释为什么你在后面将 `Bytes` 传出 WASM 的时候需要在地址上 `+ 8`，因为这是对象头的大小。

* * *

> 以上 FFI 文档说`Bytes` 的 API 不稳定，那这种做法恐怕过几个版本就可能失效了？有没有更可靠的办法呢？

你可以通过 heap-start-address 来指定 MoonBit 堆的最低地址，然后在低于该地址的地方把 Bytes 写入进去。比如说你可以定义一个这样的 FFI ：

```auto
extern "wasm" fn write_byte(address : Int, byte : Byte) = ...

```

然后通过逐个遍历 Bytes 中的字节的方式将内容写入到固定的地址。

---

<div class="post-metadata">

作者： ![ChenYubin](https://taolun.moonbitlang.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@ChenYubin](https://taolun.moonbitlang.com/u/ChenYubin)\
发布日期： [2026 年3 月 11 日 09:16 UTC](https://taolun.moonbitlang.com/t/topic/1397/3 "2026-03-11T09:16:02Z")

</div>

感谢你这么详细的反馈！

1. 关于版本的问题

2. `moon fmt`转换出的`moon.pkg`不稳定

3. 关于文档

4. taolun 和 discuss 的关系

---

<div class="post-metadata">

作者： ![YDX](https://taolun.moonbitlang.com/user_avatar/taolun.moonbitlang.com/ydx/32/860_2.png) [@YDX](https://taolun.moonbitlang.com/u/YDX)\
发布日期： [2026 年4 月 26 日 15:42 UTC](https://taolun.moonbitlang.com/t/topic/1397/4 "2026-04-26T15:42:17Z")

</div>

> [@ChenYubin](#):
>
> taolun是中文论坛，discuss是英文论坛。没有取代和继承关系。

现在好像无法访问 [https://discuss.moonbitlang.com/](https://discuss.moonbitlang.com/) 了，因为变成原地 301 Moved Permanently 了。

---

<div class="post-metadata">

作者： ![YDX](https://taolun.moonbitlang.com/user_avatar/taolun.moonbitlang.com/ydx/32/860_2.png) [@YDX](https://taolun.moonbitlang.com/u/YDX)\
发布日期： [2026 年6 月 13 日 11:49 UTC](https://taolun.moonbitlang.com/t/topic/1397/5 "2026-06-13T11:49:49Z")

</div>

> [@YDX](#):
>
> `以上加八是试验出来的`

[`v0.9.2+bbe2b338f`](https://www.moonbitlang.com/updates/2026/05/13/index)还需要加八，但[`v0.10.0+84519ca0a`](https://www.moonbitlang.com/updates/2026/06/08/moonbit-0-10-0-release)、`0.10.0+e66899a54`又不需要加八了。不过我没在更新日志里找到相关描述……

背景：[typst-community/wasm-minimal-protocol#75](https://github.com/typst-community/wasm-minimal-protocol/pull/75)
