Skip to Content
Course content

88: New Syntax: Significant Indentation

Click on the "Edit" button in the top corner of the screen to edit your slide content.

If you're coming from Scala 2, or if you've spent a lot of time in Java or C#, you probably have a deeply ingrained reflex: every time you write a function or an if statement, your fingers automatically hit the { key. You might think that indentation in Scala is just a professional courtesy—something we do so our teammates don't hate us—but that the compiler only truly cares about those curly braces.

The Myth: Indentation is just for the humans

A lot of learners assume that because Scala 3 introduced "significant indentation," it's just a syntactic sugar that behaves like a "suggestion." They think they can remove the braces and then indent their code however they like, as long as it "looks" roughly correct to the eye. Let's look at why that's a dangerous assumption.

def calculateShipping(weight: Double): Double =
  if weight > 10.0
    println("Heavy package")
    15.0
  else
    5.0

If you wrote this thinking the compiler is just "guessing" where the if block ends, you're in for a surprise. In the snippet above, the println is indented, but the 15.0 is also indented at the same level. You might expect both to be part of the if block. But if you accidentally shift that 15.0 one space to the left, the entire logic of your program changes—or more likely, the compiler will throw a fit because the block structure is now ambiguous.

The Reality: The first line sets the anchor

In Scala 3, when you omit the curly braces, the indentation of the first statement after the colon or the control keyword becomes the "anchor." Every subsequent line that is indented to that same level (or deeper) is considered part of that same block. The moment you "outdent" (move the code back to the left), you've told the compiler the block is over.

I personally find this makes the code much cleaner, but you have to be disciplined. Look at this refined version of a discount calculator:

def applyDiscount(price: Double, coupon: String): Double =
  if coupon == "SUMMER24"
    println("Applying summer discount!")
    price * 0.8
  else if coupon == "WELCOME10"
    println("Applying welcome discount!")
    price * 0.9
  else
    price

Notice how the println and the math operation are perfectly aligned. That alignment is what tells Scala, "These two lines belong together in this specific branch." If I were to move the price * 0.8 back to the same column as the if, it would be outside the if block entirely, and the code wouldn't compile because the if expression would be missing its required value.

A quick tip: don't try to be clever by mixing tabs and spaces. I can't stress this enough. Scala 3 expects consistency. If you start a block with two spaces, stick to two spaces. If you mix them, you'll get a compiler error that tells you the indentation is inconsistent, and it's a nightmare to debug visually.

  • Braces are still there: You can still use { } whenever you want. They are not deprecated.
  • Consistency is king: The first line of the block defines the indentation level for every other line in that block.
  • Clarity over brevity: If a block is complex, I often go back to using braces just to be explicit. There's no shame in that.



📋 Practical Task

Refactoring the Order Validator to Brace-less Syntax

Below is a piece of code that uses traditional curly braces to validate a customer's order. Your task is to refactor this function to use significant indentation instead of braces. Ensure that the logic remains identical and the indentation correctly defines the scopes of the if and else blocks.

def validateOrder(quantity: Int, pricePerUnit: Double): String = {
  val total = quantity * pricePerUnit
  if (total > 1000.0) {
    println("Order requires manager approval")
    "PENDING"
  } else if (total <= 0) {
    println("Invalid order amount")
    "REJECTED"
  } else {
    println("Order accepted")
    "APPROVED"
  }
}

Requirements:

  1. Remove all curly braces { }.
  2. Ensure the val total assignment is part of the function body.
  3. Ensure the println and the resulting string (e.g., "PENDING") are correctly aligned to stay within their respective if/else blocks.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.