I would rather not introduce a new language construct for that specific purpose.
I’m sympathetic to the idea of supporting top-level def definitions, and my suggestion is to change the entry point detection algorithm to support not only top-level objects having a main
method taking an Array[String]
and returning Unit
, but also top-level methods named main
(optionally taking an Array[String]
). Here are some examples:
// example1.scala
def main = println("hello, world")
// example2.scala
package foo
def main(args: Array[String]) = println(s"hello, ${args(0)}")
// example3.scala
package foo
object Main {
def main(args: Array[String]) = println(s"hello, ${args(0)}")
}
Invoking scala example1.scala
would execute the entry point, if exactly one entry point is found.
Last, the JVM-linker should expose a means of configuring the name of the wrapping object, when the entry point is a top-level main
def:
// build.sbt for example2.scala source file
Compile / entryPoint := EntryPoint.topLevelMethod("foo.main", jvmWrappingClass = "foo.Main")
The default would be to use the default wrapping object, which is <empty>$package
, foo$package
, and foo.Main
, in the above examples.