Being a hybrid object/functional language, Scala offers both recursion and imperative control structures (like
while, etc) to express iteration. However, imperative control statements imply using mutable state, while Scala (and FP in general) encourages data immutability. That is why you may prefer to use recursion (or higher order functions) in the first place.
To facilitate the FP approach to recursion, Scala offers:
So far so good. Yet the underlying platform (i.e. JVM) still uses up a call stack for non-tail recursive methods, so we can get a nasty stack overflow in no time while processing reasonably large amounts of data. Therefore it is a good idea to know where recursion lurks (and to easily distinguish its type).
While, in real-life code, recursion is not always obvious, new icons help us to reveal it:
Let’s (for the sake of example) rewrite our linear recursion to tail recursion:
We can use an intention (Alt + Enter) to automatically add
@tailrec annotation which guarantees that compiler will always optimize the method (or will refuse to compile it).
After we (accidentally) change the method back to the linear recursion, we can immediately notice the violation of the contract:
If you wish, you may enable an optional “No @tailrec annotation inspection” which suggests to add
@tailrec annotation when needed.
IntelliJScala @ Scala Days 2023 Madrid
On September 12 – 14, the Scala Plugin team was in Madrid for the Scala Days conference. JetBrains was a Gold Sponsor for the event and we are very proud of it. We came to Madrid with a booth, merchandise, and a talk about the IntelliJ Scala Plugin. The Scala Plugin has seen a lot of improvements…
IntelliJ Scala Plugin 2023.2 Is Out!
Better Scala 3 Support IntelliJ IDEA 2023.2 brings enhanced Scala 3 support, with a focus on providing a streamlined development experience. Notable improvements include fixes for Scala 3 enum highlighting, navigation to enum definitions, and the correct resolution of enum cases in various contex…
IntelliJ Scala Plugin 2023.1 Is Out!
Improved support for braceless Scala syntax IntelliJ IDEA can now desugar braceless Scala code properly, and it handles refactorings where the “fewer braces” feature is used. It also supports braceless syntax in worksheets and correctly interprets indentation when you move extensions methods up a…
IntelliJ Scala Plugin 2022.3 Is Out!
This release has been again focused on Scala 3, but there are also quite a few other improvements. Better Scala 3 support In v2022.3, we’ve introduced a large number of upgrades to provide better Scala 3 support. The IDE now supports parameter untupling and quoted patterns, and it features man…