Scala 3 · 37 of 42
In Scala 2, everything had to live inside a class, trait, or object. A simple script meant ceremony:
object Main {
def main(args: Array[String]): Unit = {
println("Hello!")
}
}
Scala 3 removes both layers of ceremony:
vals, defs, type aliases, and givens can sit directly
in a file, next to your classes. (This replaces Scala 2’s “package objects”, which
are being phased out.)@main methods: any method annotated with @main becomes a program entry point.
Its parameters become command-line arguments, parsed for you:@main def greet(name: String, times: Int) =
for _ <- 1 to times do println(s"Hello, $name!")
Running scala run . -- Alice 3 prints the greeting three times; no Array[String]
in sight.
@main is shown in a comment (a worksheet has
no place for a program entry point; a plain .scala file does).
//In Scala 3, definitions can live directly at the top of a file;
//no wrapper object needed.
val language = "Scala 3"
def welcome(name: String) = s"Welcome to $language, $name!"
case class User(name: String)
println(welcome(User("Alice").name))
//In a real .scala file, an entry point is just an annotated method:
// @main def run(): Unit = println(welcome("world"))
//(this worksheet already runs top-to-bottom like a script,
// so here we just call the method directly)
def run(): Unit = println(welcome("world"))
run()