E4068

E4068#

Main function is already defined.

In MoonBit, a main package contains a single main function that serves as the entry point of the program. Therefore, you cannot define more than one main function in the main package. If you want to have multiple programs to be built as separate binaries, you can use multiple main packages within the same module.

错误示例#

Suppose in package main:

main/moon.pkg.json:

{
  "is-main": true
}

main/main.mbt:

fn main {
  println("Hello, World!")
}

fn main { // Error: Main function is already defined at ...
  println("Hello, World! Again!")
}

建议#

You can either remove the extra main function:

fn main {
  println("Hello, World!")
}
// Remove the extra main function
//- fn main {
//-   println("Hello, World! Again!")
//- }

Or you can move the extra main function to a different package, say main2:

main2/moon.pkg.json:

{
  "is-main": true
}

main2/main.mbt:

fn main {
  println("Hello, World! Again!")
}

while keeping the main/main.mbt contains only one main function:

fn main {
  println("Hello, World!")
}