MoonBit recently introduced virtual package feature. Users can declare a package as virtual and define interfaces via .mbti
files to choose implementation. If no implementation is specified, default is used. This provides flexibility in separating interfaces from implementations (experimental).
Example project structure:
.
├── virtual // Declared as virtual with virtual.mbti defining interfaces
├── impl // Custom implementation for virtual package interfaces
├── lib // Depends on virtual package interfaces
└── main // Depends on lib and overrides virtual package default implementation with impl
Declaring a virtual package: In moon.pkg.json
of virtual
package, set "virtual": {"has-default": true}
. Package must include .mbti
file.
Providing custom implementation: In moon.pkg.json
of impl
package, set "implement": "username/hello/virtual"
. Implementation must satisfy virtual package .mbti
file.
Using a virtual package: In moon.pkg.json
of lib
package, add virtual package in "import"
field.
Overriding a virtual package: In moon.pkg.json
of main
package, specify implementation package in "overrides"
field. If no implementation and default, default is used; if no default, error.
Example in moonbitlang/core
:
abort.mbti
declares APIs.abort.mbt
provides default implementation withpanic_impl
. Current default ignoresmsg
to avoid depending onspectest::print_char
in other runtimes.
Customizing implementation: Run moon add moonbitlang/dummy_abort
and add "overrides": ["moonbitlang/dummy_abort/abort_show_msg"]
to moon.pkg.json
. Now abort
passes and prints the parameter.
Intentionally misusing swap
on an array shows an error message. In the future, MoonBit community plans to provide WASI standard compliant libraries for println
to run on WebAssembly runtimes.
New to MoonBit:
- Download MoonBit: https://aka.moonbitlang.com/vsh
- Explore MoonBit Beginner's Guide: https://docs.moonbitlang.com/en/latest/tutorial/tour.html
- Play with MoonBit Language Tour: https://tour.moonbitlang.com/
- Check out MoonBit Docs: https://docs.moonbitlang.com/en/latest/index.html
- Join Discord community: https://discord.gg/5d46MfXkfZ
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。