Introduction
With Scalafix, you can focus on the changes that truly merit your time and attention rather than having to deal with easy, repetitive, and tedious code transformations. Scalafix is the best code migration tool for Scala and transforms unsupported features in source files into newer alternatives and writes the result back to the original source file. Scalafix may not be able to perform automatic migrations in some cases. When this happens, Scalafix will provide instructions on how to refactor the code and point to the offending source line. With new Scala versions, you’ll be able to get up and running faster for your existing Scala code base.
Scalafix and Scala 2
Scala 2.13 is still being planned, so we don’t know what exactly language changes it will include. But with the help of Scalafix available, we can easily migrate our existing code and changes could become possible that would previously have been considered too disruptive because they would require too much code to be updated by hand. Possible examples include:
We can rewrite code to avoid the use of the now-disfavored language.postfixOps syntax
Deprecation of any2StringAdd, in favour of string interpolation
Deprecation of procedure syntax, as in Dotty
The changes have been made to the Scala collections API
Scalafix and Dotty
Among the new features of Dotty are faster compilation times, faster + smaller binaries, and awesome error messages. Due to several breaking changes in Dotty, some Scala 2.x applications cannot immediately take advantage of these benefits. We use Scalafix to streamline the migration process for those applications.
What exactly scalafix do?
Scalafix comes with a command-line interface and also with an SBT plugin. Running scalafix is as simple as:
# CLI
scalafix Code.scala
# SBT, after adding plugin to project/plugins.sbt
sbt scalafix
ProcedureSyntax and VolatileLazyVal have been rewritten in this initial release.
The ProcedureSyntax
rewrite works like this:
// before
def main(args: Seq[String]) { // note lack of '='
println("Hello scalafix!")
}
// after
def main(args: Seq[String]): Unit = { // note ': Unit ='
println("Hello scalafix!")
}
In an effort to simplify the Scala language, procedure syntax is being deprecated or dropped.
The VolatileLazyVal
rewrite adds Dotty’s @volatile
annotation to lazy vals, as somethings like this:
lazy val x = ... // before
@volatile lazy val x = ... // after
Dotty uses @volatile to implement a deadlock-free scheme that is comparable to or even faster than Scalac’s.
How does scalafix work?
Scalafix is built using scala.meta, an upcoming metaprogramming toolkit intended to succeed scala. reflect.Scala.meta makes it very easy to do non-trivial code transformations while preserving syntactic details in the original source file. The key attribute of scala.meta also makes it very suitable for both developer tools like scalafix as well as compile-time metaprograms like macros.
Conclusion
Metaprograms are untitled from compiler implementation by providing a platform-independent API that can be used in both Scala compilers, Dotty and tools like IntelliJ IDEA. Developers benefit from an intuitive user interface built on immutable ADTs and type classes. Compiler authors also get benefit from the fact that they don’t need to expose compiler internals in order to support popular features like macros.
For more details visit here