AnatomyOf

Anatomy of a Scala file

example.scala
1import scala.util.Random // for embracing chaos responsibly
2
3object Dice { // an object of one, the loneliest instance
4 val sides: Int = 20 // no save throws in production code
5 def roll(): Int = Random.nextInt(sides) + 1
6}
7
8object Main {
9 def main(args: Array[String]): Unit = {
10 val result = Dice.roll()
11 val verdict = if (result > 10) "critical hit" else "critical miss" // it is always a miss
12 println(s"You rolled $result: $verdict")
13 }
14}
Scala is a statically typed, multi-paradigm language that combines object-oriented and functional programming, most commonly targeting the JVM.