You need a fast edit-run loop before learning anything else.
Since Scala 3.5 the scala command is Scala CLI: one tool that compiles, runs, tests and packages code. Install it (with Coursier, Homebrew or SDKMAN), then scala run hello.scala compiles and runs a file, scala repl (or just scala) opens the interactive REPL, and scala test . runs tests. Larger projects use sbt, Mill or Gradle, covered later.
A program's entry point is any top-level method annotated @main. Its parameters are parsed from the command line, so @main def greet(name: String, times: Int) can be run with scala run greet.scala -- Ana 2; the compiler generates the parsing and an error message for bad input.
Scala 3 code can be written with significant indentation (Python-like, the default style) or with braces; both compile. if/then, while/do and for/do read naturally without parentheses. An end marker such as end greet is optional and helps in long blocks.
Directives at the top of a file configure Scala CLI: //> using scala 3.7 picks the version and //> using dep com.lihaoyi::os-lib:0.11.4 adds a library, so a single file can be a complete script.
Top-level definitions (methods, values, types) are allowed in Scala 3; no wrapper object is needed. Files ending in .sc are scripts that may also contain top-level statements, run in order; the short examples in these lessons that have no @main are written that way (or paste them into the REPL).
Example
//> using scala 3.7
// run: scala run hello.scala -- Ana 2
@main def greet(name: String, times: Int): Unit =
for i <- 1 to times do
println(s"Hello, $name! ($i)")
// Output:
// Hello, Ana! (1)
// Hello, Ana! (2)Remember
scala run file.scalacompiles and runs;scalaalone opens the REPL@mainmethods are entry points with parsed arguments- Indentation syntax and braces are both valid Scala 3
//> usingdirectives declare the Scala version and dependencies
Watch out for
- Forgetting
--before program arguments, so Scala CLI tries to interpret them as its own options.