Typical compiler options for developers

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:

  1. Is there a (curated) list which options should be used? Like a best-practice?
  2. How can I integrate those options? Do I add it to my build.sbt?
    2a. Does anyone know how to enforce them in IntelliJ?
2 Likes

Coming from Java I am not used to that

Really?
I believe Java also has those, maybe they are not that common?

Anyways…

Is there a (curated) list which options should be used

Yes!
But wait, there are better news coming…

How can I integrate those options? Do I add it to my build.sbt ?

Yes, you set them manually as the blog post shows or, you can just use sbt-tpolecat and call it a day. :smiley:
This is great because:

  1. The plugin is updated, whereas the blog post isn’t.
  2. The plugin automatically handles cross-compilation.
  3. 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.

2 Likes

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 -Xlint.

And if you can, enable -Werror (also known as -Xfatal-warnings), at least on CI if not for local development.

Note that -Xlint and -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 -Wconf and @nowarn, which allow you to suppress warning types and individual warnings that you don’t care about.

2 Likes

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+)
1 Like

-Xfatal-warnings and -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 -Wconf:any:error.

4 Likes