Scala Compiler Tuning

Post image

As my Scala projects go on, I want to share some compiler configuration and tricks with you, which I use on many projects. Some tiny configuration options can greatly improve your code and warn you about things, you would probably never discover.

Basically, you can pass compiler options to scalac using console arguments:

$ scalac -deprecation -unchecked -Xlint something.scala

If you are using SBT, it’s even simpler… You can just use the following configurations snippet in your build.sbt file to add scala options:

scalacOptions ++= Seq("-deprecation", "-unchecked", "-Xlint")

Common Scalac Options

There are some common options which I’d like to share with you. Most of them make it harder to compile “not that good” code or warn about certain conditions:

Option Description
-deprecation Emit warning and location for usages of deprecated APIs.
-feature Emit warning and location for usages of features that should be imported explicitly.
-unchecked Enable additional warnings where generated code depends on assumptions.
-Xfatal-warnings Fail the compilation if there are any warnings.
-Xlint Enable recommended additional warnings.
-Ywarn-adapted-arg Warn if an argument list is modified to match the receiver.
-Ywarn-dead-code Warn when dead code is identified.
-Ywarn-inaccessible Warn about inaccessible types in method signatures.
-Ywarn-nullary-override Warn when non-nullary overrides nullary, e.g. def foo() over def foo.
-Ywarn-numeric-widen Warn when numerics are widened.
-Ywarn-value-discard Warn when non-Unit expression results are unused.
-Ywarn-unused arn when local and private vals, vars, defs, and types are unused.

This is just a very small subset of available options for the Scala compiler. If you want to see all options available, just ask your scalac for it. To show all the options, just run scalac -X or scalac -Y. You can also just display the compiler help using scalac -help.

Finally, my personal Scala options (as SBT settings) for most of the Scala projects I currently work on:

// compiler tuning for the win - makes it harder to build schwurbl
scalacOptions ++= Seq(

Of course there are also such options for javac, the Java compiler. Just run javac -help or javac -X

As always, I really apreciate feedback of all kinds, or just send me your compiler configuration…

You May Also Like

Hiring Devs - The Seniority Paradox

Hiring Devs - The Seniority Paradox

In IT, there is a paradox when it comes to job titles. Somewhere along the way, the practice of linking job titles to a candidate’s (desired) experience became established in large parts of the market. This then created the now famous and omnipresent job titles like “Senior Software Engineer”, “Professional DevOps Engineer”, etc. In this blog post, I would like to explain the paradox around these designations and how I use them in the course of the talent search…

Blog Relaunch (Yes, again)

Blog Relaunch (Yes, again)

I noticed that I write not as often as I want to, so I procrastinated and first relaunched my blog instead of writing. It has a new design and some new functionality, also the posts have now images. The relaunch was, for me, some kind of personal retrospective, where I read all my old posts and found out that I’m probably a better webmaster than a writer…