Scala lets you add new methods to existing classes that you don’t have the source code for, i.e., classes like
Int, etc. For instance, you can add a method named
hello to the
String class so you can write code like this:
which yields output like this:
Admittedly that’s not the most exciting method in the world, but it demonstrates the end result: You can add methods to a closed class like
String. Properly (tastefully) used, you can create some really nice APIs.
In this article I’ll show how you can create implicit methods (also known as extension methods) in Scala 2 and Scala 3 (Dotty).
If you’re interested in the future of Scala, i.e., Scala 3, also known as Dotty, Martin Odersky recently shared a working draft document titled, Functional Typelevel Programming in Scala. See the “Files Changed” link on that page for the complete working document.