Significant Indentation
Optional braces.
Significant Indentation is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Optional Braces
Scala 3 introduces significant indentation, also called the optional braces syntax. Instead of wrapping blocks in { }, you can use indentation to delimit them.
- Curly braces still work and are fully valid.
- Indentation-based code is cleaner and Python-like.
object Main:
def main(args: Array[String]): Unit =
println("No braces needed")Indented Method Bodies
A method body that follows = can be an indented block. Every statement at the same indentation level belongs to that body.
The last expression is the return value.
object Main:
def square(x: Int): Int =
val result = x * x
result
def main(args: Array[String]): Unit =
println(square(5))Indented if Expressions
Control structures use the new quiet syntax. After if cond then you indent the branch, and else aligns with if.
thenis required in the brace-free style.- No parentheses around the condition are needed.
object Main:
def main(args: Array[String]): Unit =
val n = 7
val label =
if n % 2 == 0 then "even"
else "odd"
println(label)Indented for Loops
The for loop uses do for side effects or yield to build collections, both with indentation.
object Main:
def main(args: Array[String]): Unit =
for i <- 1 to 3 do
println(s"line $i")
val doubled =
for i <- 1 to 3 yield i * 2
println(doubled)Indented while Loops
The while loop uses do followed by an indented block in the optional-braces style.
object Main:
def main(args: Array[String]): Unit =
var i = 0
while i < 3 do
println(s"i = $i")
i += 1Indented match Expressions
Pattern matching is naturally indentation-friendly. The case clauses are indented under match.
object Main:
def describe(n: Int): String =
n match
case 0 => "zero"
case 1 => "one"
case _ => "many"
def main(args: Array[String]): Unit =
println(describe(1))
println(describe(9))Class and Trait Bodies
Classes, traits, and objects can drop their braces too. A colon : after the header opens the indented body.
class Point(x: Int, y: Int):
def show: String = s"($x, $y)"
object Main:
def main(args: Array[String]): Unit =
println(Point(2, 3).show)The end Marker
For long indented blocks you can add an end marker to clarify where a definition closes. It improves readability for big methods or classes.
- Form:
end <name>orend if,end while, etc. - It is optional and checked by the compiler.
object Main:
def greet(name: String): String =
val msg = s"Hello, $name"
msg
end greet
def main(args: Array[String]): Unit =
println(greet("Ada"))
end main
end MainMixing Braces and Indentation
You can freely mix both styles. Some teams keep braces for clarity in nested cases. Indentation rules are only enforced where braces are absent.
object Main:
def main(args: Array[String]): Unit = {
val xs = List(1, 2, 3)
val total =
xs.foldLeft(0)((acc, x) => acc + x)
println(total)
}Common Indentation Pitfalls
Indentation is meaningful, so be consistent.
- Use spaces, not tabs mixed with spaces.
- A misaligned line can silently fall outside a block.
- The colon
:is needed for template bodies (classes/objects), but not for method bodies after=.
object Main:
def main(args: Array[String]): Unit =
val items = List("a", "b")
items.foreach: item =>
println(item.toUpperCase)Fewer-Braces Argument Syntax
Scala 3 also allows passing a block argument after a colon. items.foreach: x => opens an indented lambda body, avoiding nested parentheses and braces.
object Main:
def main(args: Array[String]): Unit =
val nums = List(1, 2, 3, 4)
val evens = nums.filter: n =>
n % 2 == 0
println(evens)Quick Check
Test your understanding of significant indentation.
Recap
You learned Scala 3 significant indentation.
- Blocks can use indentation instead of
{ }. - Control structures use
then,do, andyield. - Class/object/trait bodies open with a colon
:. endmarkers clarify long blocks.- Both styles can be mixed; stay consistent with spacing.
object Main:
def main(args: Array[String]): Unit =
for i <- 1 to 2 do
if i == 1 then println("first")
else println("second")
end main
end MainFrequently asked questions
Is the “Significant Indentation” lesson free?
Yes — the full text of “Significant Indentation” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Significant Indentation”?
Optional braces. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Significant Indentation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Significant Indentation
- Enums in Scala 3
- Opaque Types
- Union and Intersection Types