Just recently I learned, that Scala developers use compiler options to be warned about deprecation, feature warnings, and so on.
Coming from Java I am not used to that, and so I have some question regarding compiler options:
- Is there a (curated) list which options should be used? Like a best-practice?
- How can I integrate those options? Do I add it to my
2a. Does anyone know how to enforce them in IntelliJ?
Coming from Java I am not used to that
I believe Java also has those, maybe they are not that common?
Is there a (curated) list which options should be used
But wait, there are better news coming…
How can I integrate those options? Do I add it to my
Yes, you set them manually as the blog post shows or, you can just use sbt-tpolecat and call it a day.
This is great because:
- The plugin is updated, whereas the blog post isn’t.
- The plugin automatically handles cross-compilation.
- If you use something like scala stedward you will have the plugin always updated.
Does anyone know how to enforce them in IntelliJ?
No idea, sorry. I do not use IntelliJ.
I believe that if you import the build IntelliJ should identify and respect them, but not sure.
I just use metals + vscdoe which does import all those options.
BTW: Even if the plugin will configure all the flags for you, I still recommend you to read the blog and understand all the flags.
sbt-tpolecat is a good option.
If you don’t use it, then by far the most important single piece of advice is to at least enable
And if you can, enable
-Werror (also known as
-Xfatal-warnings), at least on CI if not for local development.
-Werror are both javac flags as well; they are not Scala-only.
The doc page on scalac options is https://docs.scala-lang.org/overviews/compiler-options/index.html
Note that Scala 2.13.3 added
@nowarn, which allow you to suppress warning types and individual warnings that you don’t care about.
I have a related question:
Do following three flags behave exactly the same? Are there any differences?
-Xfatal-warnings (Scala <2.13, but also works in Scala 2.13+)
-Werror (Scala 2.13+)
-Wconf:any:error (2.13.2+ and 2.12.13+)
-Werror are the same,
-W is new for everything warnings in 2.13, but the old flags continue to work.
-Wconf:any:error changes every warning to be an error, which causes the compiler to stop after the phase where the first warning occurs.
-Werror issues warnings as warnings and runs the compilation pipeline to the end, including the generation of classfiles. It issues a single error message at the end and quits with a non-zero exit code.
Interestingly, zinc special-cases
-Werror and issues every warning as an individual error. So in sbt,
-Werror behaves like