Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active October 28, 2025 13:10
Show Gist options
  • Select an option

  • Save mizchi/957e3156816d1f1be8279a103ac09f6b to your computer and use it in GitHub Desktop.

Select an option

Save mizchi/957e3156816d1f1be8279a103ac09f6b to your computer and use it in GitHub Desktop.
```mbt
async fn[T, U] map_async(arr: Array[T], f: async (T) -> U raise?) -> Array[U] raise? {
let res: Array[U] = []
for item in arr {
res.push(f(item))
}
res
}
async test {
let arr = [1, 2, 3, 4, 5]
let xs = map_async(arr, async fn(x: Int) -> Int raise {
if x == 404 {
fail("error at 404")
}
x * 2
})
assert_eq(xs, [2, 4, 6, 8, 10])
}
```
moon test --target native
```
failed: moonc link-core /Users/mizchi/.moon/lib/core/target/native/release/bundle/abort/abort.core /Users/mizchi/.moon/lib/core/target/native/release/bundle/core.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/c_buffer/c_buffer.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/os_error/os_error.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/fd_util/fd_util.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/time/time.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/coroutine/coroutine.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/event_loop/event_loop.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/stdio/stdio.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/io/io.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/semaphore/semaphore.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/fs/fs.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/aqueue/aqueue.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/async.core /Users/mizchi/sandbox/myapp/target/native/debug/test/myapp.inline_test.core -main mizchi/myapp -o /Users/mizchi/sandbox/myapp/target/native/debug/test/myapp.inline_test.c -test-mode -pkg-config-path /Users/mizchi/sandbox/myapp/src/moon.pkg.json -pkg-sources moonbitlang/async/internal/c_buffer:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/c_buffer -pkg-sources moonbitlang/async/os_error:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/os_error -pkg-sources moonbitlang/async/internal/fd_util:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/fd_util -pkg-sources moonbitlang/async/internal/time:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/time -pkg-sources moonbitlang/async/internal/coroutine:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/coroutine -pkg-sources moonbitlang/async/internal/event_loop:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/event_loop -pkg-sources moonbitlang/async/stdio:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/stdio -pkg-sources moonbitlang/async/io:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/io -pkg-sources moonbitlang/async/semaphore:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/semaphore -pkg-sources moonbitlang/async/fs:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/fs -pkg-sources moonbitlang/async/aqueue:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/aqueue -pkg-sources moonbitlang/async:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src -pkg-sources mizchi/myapp:/Users/mizchi/sandbox/myapp/src -pkg-sources moonbitlang/core:/Users/mizchi/.moon/lib/core -exported_functions 'moonbit_test_driver_internal_execute,moonbit_test_driver_finish' -target native -O0
-- --
/ // / __--------_
/ // /_/ \
--- - \ __
/ X / ____ / )
*_________/__/_____/______/ `--
Oops, the compiler has encountered an unexpected situation.
This is a bug in the compiler.
A bug report containing the error description and relevant code would be
greatly appreciated. You can submit the bug report here:
https://github.com/moonbitlang/moonbit-docs/issues/new?template=ice.md
Error: File "moonc.ml", line 172556, characters 33-39: Assertion failed
Compiler args: moonc link-core /Users/mizchi/.moon/lib/core/target/native/release/bundle/abort/abort.core /Users/mizchi/.moon/lib/core/target/native/release/bundle/core.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/c_buffer/c_buffer.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/os_error/os_error.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/fd_util/fd_util.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/time/time.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/coroutine/coroutine.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/internal/event_loop/event_loop.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/stdio/stdio.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/io/io.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/semaphore/semaphore.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/fs/fs.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/aqueue/aqueue.core /Users/mizchi/sandbox/myapp/target/native/debug/test/.mooncakes/moonbitlang/async/async.core /Users/mizchi/sandbox/myapp/target/native/debug/test/myapp.inline_test.core -main mizchi/myapp -o /Users/mizchi/sandbox/myapp/target/native/debug/test/myapp.inline_test.c -test-mode -pkg-config-path /Users/mizchi/sandbox/myapp/src/moon.pkg.json -pkg-sources moonbitlang/async/internal/c_buffer:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/c_buffer -pkg-sources moonbitlang/async/os_error:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/os_error -pkg-sources moonbitlang/async/internal/fd_util:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/fd_util -pkg-sources moonbitlang/async/internal/time:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/time -pkg-sources moonbitlang/async/internal/coroutine:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/coroutine -pkg-sources moonbitlang/async/internal/event_loop:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/internal/event_loop -pkg-sources moonbitlang/async/stdio:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/stdio -pkg-sources moonbitlang/async/io:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/io -pkg-sources moonbitlang/async/semaphore:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/semaphore -pkg-sources moonbitlang/async/fs:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/fs -pkg-sources moonbitlang/async/aqueue:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src/aqueue -pkg-sources moonbitlang/async:/Users/mizchi/sandbox/myapp/.mooncakes/moonbitlang/async/src -pkg-sources mizchi/myapp:/Users/mizchi/sandbox/myapp/src -pkg-sources moonbitlang/core:/Users/mizchi/.moon/lib/core -exported_functions moonbit_test_driver_internal_execute,moonbit_test_driver_finish -target native -O0
moonc version: v0.6.29+9037370fc
```
## It works
```mbt
async fn[T, U] map_async(v: T, f: async (T) -> U raise?) -> U raise? {
f(v)
}
async test {
let r = map_async(1, async fn(x: Int) -> Int raise {
if x == 404 {
fail("error at 404")
}
x * 2
})
assert_eq(r, 2)
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment