Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Setting Up Scala  ›  Lesson

Your First Program

Setting Up Scala 12 minYou install Scala with Coursier, write a .scala file containing a function marked @main, and run it with scala run, which compiles and starts it in one step.

Your First Program

The club secretary wants to try Scala today, not next week. Getting from nothing to a running program takes three things: the tools, one file and one command. Once those work, every later lesson is just more lines in that file.

The rule

Install once, write a @main function, run it with scala run.

Install once. Scala's official installer is Coursier, a command called cs. Download it for your system from the Scala website, then run cs setup. It installs a Java runtime if you do not have one, plus the scala command and related tools. Check that it worked:

scala --version

Write a @main function. Create a file called hello.scala:

@main def hello(): Unit =
  println("Riverside Run Club: logbook open")
  • def hello() defines a function named hello with no parameters.
  • @main marks it as the place where the program starts. Without it, there is nothing to run.
  • : Unit says the function returns no useful value; it is run for its effect, printing a line.
  • The body is the indented line after =. Scala 3 uses the indentation to see where the function ends.

Run it with scala run.

scala run hello.scala

The command compiles the file to JVM bytecode and then starts it. The first run is slower because the compiler is downloaded and cached; later runs reuse it. To compile without running, use scala compile hello.scala.

A @main function can take parameters, and Scala fills them from the words you type after --:

@main def welcome(name: String): Unit =
  println(s"Welcome to the club, $name")
scala run welcome.scala -- Asha

This prints Welcome to the club, Asha. Leave the name out and the program stops with a message that more arguments were expected.

The shortcut

Treat setup as a loop with four beats: check, write, run, change. If a beat fails, the error tells you which beat to repeat.

From an empty folder to a greetingStep 1 of 4
  1. Check

    scala --version prints a version number. If the command is not found, rerun cs setup and open a new terminal.

The common first-day mistakes are a missing @main, so the runner finds no entry point, and program arguments placed before --, where the scala command treats them as more files to compile.

Drills

Answer fast — trust the rule.

quick drill

What does the @main annotation do?

quick drill

Which command compiles and runs hello.scala?

quick drill

@main def welcome(name: String): Unit — how do you pass the name Asha?

quick drill

What does the return type Unit tell you about hello()?

quick drill

What is cs setup for?

Remember

  • Install with cs setup, write a @main function, run it with scala run file.scala.
  • Shortcut: check, write, run, change.
  • Trap: no @main means no entry point, and program arguments go after --.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFrom a fresh laptop to a greeting

Walk a new club member from a fresh laptop to a Scala program that greets them by name. Explain each step, what each part of the @main def welcome(name: String): Unit = line means, and how the name reaches the program.