Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
88: New Syntax: Significant Indentation
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:
- Remove all curly braces
{ }. - Ensure the
val totalassignment is part of the function body. - Ensure the
printlnand the resulting string (e.g.,"PENDING") are correctly aligned to stay within their respectiveif/elseblocks.
There are no comments for now.