E4069#
非 main 包中出现 main
函数。
In MoonBit, packages are divided into two types: main packages and non-main
packages. Non-main packages are often used as libraries to provide functionality
to other packages. Main packages, on the other hand, are the entry points of the
program. Therefore, non-main packages should not contain a main
function.
Sometimes, people use the main function for testing. In such cases, we recommend
using the test
block to write tests, instead of using the main
function. See
the test section of the MoonBit documentation for
more information.
错误示例#
Suppose in package lib
:
lib/moon.pkg.json
:
{} // "is-main" is by default false
lib/lib.mbt
:
fn main { // Error: Unexpected main function in the non-main package.
println("Hello, world!")
}
建议#
You can remove the main
function from the non-main package:
// Remove the main function
//- fn main {
//- println("Hello, world!")
//- }
Alternatively, you can set the package to be a main package by setting
"is-main"
to true
in the package configuration file.
lib/moon.pkg.json
:
{
"is-main": true
}
You can also move the main
function to a different package, say main
:
main/moon.pkg.json
:
{
"is-main": true
}
main/main.mbt
:
fn main {
println("Hello, world!")
}