Scala Syntax
Scala 3 code looks lighter than most JVM languages. Semicolons are optional, types are usually inferred, and blocks can be written with indentation instead of curly braces. This chapter walks through the building blocks you will see in every program.
Optional braces
Scala 3 lets you mark blocks by indentation, a bit like Python. A colon : or a keyword such as then, do or = opens a block, and the indented lines that follow belong to it.
@main def run(): Unit =
val hour = 14
if hour < 12 then
println("Good morning")
else
println("Good afternoon")
for i <- 1 to 3 do
println(s"Line $i")Good afternoon
Line 1
Line 2
Line 3
The same program with braces is still valid Scala 3 — and it is what you will see in Scala 2 code:
object Main {
def main(args: Array[String]): Unit = {
val hour = 9
if (hour < 12) {
println("Good morning")
} else {
println("Good afternoon")
}
}
}Good morning
Tip: Pick one style per file. Most new Scala 3 code uses indentation; the compiler accepts both, so you can read older code without trouble.
Statements and expressions
- One expression per line; no semicolon needed. Use
;only if you put two statements on the same line. - The last expression in a block is the block's value.
- Comments use
//for one line and/* … */for several lines./** … */is a Scaladoc comment.
@main def run(): Unit =
// a single-line comment
val a = 3; val b = 4 /* two statements on one line */
val hypotenuse =
val squares = a * a + b * b
math.sqrt(squares) // the block's value
println(hypotenuse)5.0
Definitions at a glance
| Keyword | Declares | Example |
|---|---|---|
val |
an immutable value | val pi = 3.14 |
var |
a mutable variable | var count = 0 |
def |
a method | def twice(x: Int) = x * 2 |
class |
a class | class Point(x: Int, y: Int) |
case class |
an immutable data class | case class User(name: String) |
object |
a single instance | object Config |
trait |
an interface with optional code | trait Greeter |
enum |
a set of named cases | enum Color { case Red, Green } |
Type annotations
Types follow the name after a colon: name: Type. For values you can usually leave them out; for method parameters they are required.
def area(width: Double, height: Double): Double = width * height
@main def run(): Unit =
val w: Double = 2.5 // explicit type
val h = 4.0 // inferred as Double
println(area(w, h))10.0
Notice that area is defined at the top level of the file. Scala 3 allows methods, values and types outside any class or object.
Ending a block explicitly
Long indented blocks can get hard to follow. Scala 3 offers an optional end marker that names what is being closed:
object Shop:
def priceWithTax(net: Double): Double =
val rate = 0.2
net * (1 + rate)
end priceWithTax
end Shop
@main def run(): Unit =
println(Shop.priceWithTax(50.0))60.0
Naming conventions
| Thing | Style | Example |
|---|---|---|
| values, variables, methods | camelCase | totalPrice, findUser |
| classes, traits, objects, enums | PascalCase | ShoppingCart |
| constants | PascalCase (or UPPER_CASE) | MaxRetries |
| packages | lowercase | com.example.shop |
Identifiers are case-sensitive, so total and Total are different names.
Test yourself
In Scala 3 with optional braces, what defines which lines belong to an if branch?
- Semicolons
- Indentation after
then/else - The
beginkeyword - Parentheses
Which keyword declares a value that cannot be reassigned?
vardefvallet
Can a Scala 3 file contain a def outside any class or object?
- No, every method must be inside a class
- Yes, top-level definitions are allowed
- Only if it is
private - Only inside a package object