- Learning Scala Programming
- Vikash Sharma
- 187字
- 2025-04-04 17:32:11
Super smart syntax
You are going to write succinct code with Scala. There are a lot of examples we can look at to see Scala's syntax conciseness. Let's take an example from Scala's rich collections and create a Map:
val words = Map ("Wisdom" -> "state of being wise")
println(words("Wisdom"))
> state of being wise
The preceding code is creating a map of words and their meaning. Only Map ("Wisdom" -> "state of being wise") is the amount of code we have to write to make it possible. No need to add semicolons. We did not even mention the type of our value and the Scala compiler was able to infer it. Type inference is a characteristic of this language. Because of Type inference, a lot of times we omit type declaration and use a value directly. This way, using only a minimal set of words/tokens you can express the logic to implement them. Constructs like case classes and pattern matching take away the extra effort one might have to make and makes writing code joyful. It also helps you reduce written code by a good margin.