A Scala 3 “Hello, world” Example

This is a little out of order, but I want to create a small, sample application to show how easy this is with Scala 3 and scala-cli:

  • Use Terminal and VS Code
  • Create the app
  • Compile and run with scala-cli

A first example app

  • Earlier i showed how to create a function/method:
def double(i: Int): Int = i * 2
  • The way S3 works is that you annotate any method with @main to make it a “main method”. Your application starts here, and it’s called the “entry point” to your application:
// Hello.scala
@main def hello() =
    println("Hello, world")

Compile and run with scala-cli:

$ scala-cli Hello.scala

A second example app

// Hello2.scala
@main def hello(name: String) =
    println("Hello, " + name)

Run everything in the current directory with scala-cli:

$ scala-cli .

Purpose of all this

  • Show some simple applications
  • Show how to run them with scala-cli