Scala Surprises for the Java Eyes

As you start to appreciate Scala’s design elegance and conciseness, you should be aware of some nuances—for example, Scala differs semantically from Java in the way it handles assignment, equality check, and return from functions. Since the way these are handled is a significant departure from what we’re used to in Java, it’s easy to make mistakes. Take the time to learn these to avoid surprises.

Result of Assignment

In Java, the result of the assignment operation, like a = b, is the value of a, so multiple assignments like x = a = b; can appear in series, but not so in Scala. The result of assignment operation in Scala is a Unit—a rough equivalent of Void. As a consequence, assigning that result to another ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.