layout | title |
---|---|
singlepage-overview |
Scala Compiler Options |
The Scala compiler scalac
offers various compiler options, or flags, that change the compiler's default behavior. Some options just generate more compiler output in the form of diagnostics or warnings, while others change the result of compilation.
The Scala command scala
, which runs scripts or compiled code, accepts the same options as the scalac
compiler, plus a few more that determine how to run a program.
Options may be specified on the command line to scalac
or in the configuration of a build tool or IDE.
The Scala distribution includes a man
page. If Scala is installed as a system command, that documentation may be available from man scalac
.
scalac [ <options> ] <source files>
Boolean flags are specified in the usual way:
scalac -Werror -Xlint Hello.scala
Options that require arguments use "colon" syntax:
scalac -Vprint:parser,typer
Options that take just a single argument accept traditional syntax:
scalac -d /tmp
Conventionally, options have a prefix -V
if they show "verbose" output;
-W
to manage warnings; -X
for extended options that modify tool behavior;
-Y
for private options with limited support, where Y
may suggest forking behavior.
Several options have historical aliases, such as -Xfatal-warnings
for -Werror
.
In Scala 2, default paths can be listed by running a tool in the distribution:
scala scala.tools.util.PathResolver [ <options> ]
That can help debug errors in options such as --classpath
.
Here is a typical configuration of the scalacOptions
setting in sbt
:
scalacOptions ++= Seq( // use ++= to add to existing options
"-encoding", "utf8", // if an option takes an arg, supply it on the same line
"-feature", // then put the next option on a new line for easy editing
"-language:implicitConversions",
"-language:existentials",
"-unchecked",
"-Werror",
"-Xlint", // exploit "trailing comma" syntax so you can add an option without editing this line
) // for "trailing comma", the closing paren must be on the next line
The convention is always to append to the setting with ++=
and to supply one option per line.
Normally the last option will have a trailing comma so that git diff
is a bit cleaner when options are added.
{% for category in site.data.compiler-options %}
{% if category.description %}{{ category.description | markdownify }}{% endif %}-
{% for option in category.options %}
{% capture option_argument_separator %}{% if option.schema.type contains "Choice" %}:{% else %} {% endif %}{% endcapture %}
{% capture option_argument_placeholder %}{% if option.schema.arg %}{{ option.schema.arg | upcase }}{% else %}ARG{% endif %}{% endcapture %}
{% capture option_argument %}{% if option.schema.type != "Boolean" and option.schema.type != "Prefix" %}{{ option_argument_separator }}{{ option_argument_placeholder }}{% if option.schema.multiple %}1,{{ option_argument_placeholder }}2{% endif %}{% endif %}{% endcapture %}
-
{{ option.option | xml_escape }}{{ option_argument }}
{% if option.abbreviations %} {% for abbreviation in option.abbreviations %} or{{ abbreviation | xml_escape }}{{ option_argument }}
{% endfor %} {% endif %}
{% if option.deprecated %} - Deprecated: {{ option.deprecated | markdownify | remove: '
' | remove: '
'}} {% endif %}
-
{{ option.description | markdownify }}
{% if option.schema.default %}Default:
{{ option.schema.default | xml_escape }}
{% endif %} {% if option.schema.min %}Min:{{ option.schema.min }}
{% endif %} {% if option.schema.max %}Max:{{ option.schema.max }}
{% endif %}
{% if option.note %} - Note: {{ option.note | markdownify | remove: '
' | remove: '
'}} {% endif %}
{% if option.schema.choices %}
-
-
{% for choice in option.schema.choices %}
{{ option.option | xml_escape }}{{ option_argument_separator }}{{ choice.choice | xml_escape }}
{% if choice.deprecated %}- Deprecated: {{ choice.deprecated | markdownify | remove: '
' | remove: '
'}} {% endif %}
{% if choice.description %} - {{ choice.description | markdownify}} {% endif %} {% if choice.note %}
- Note: {{ choice.note | markdownify | remove: '
' | remove: '
'}} {% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
Applications or libraries targeting the JVM may wish to specify a target version.
The -release
option specifies the target version, such as "8" or "18".
Like the option for javac
, it allows building against an earlier version of the JDK. It will compile against the API for that version and also output class files for that version.
The deprecated option -target
does not compile against the desired API, but only specifies a target class file format.
- parser
- parse source into ASTs, perform simple desugaring
- namer
- resolve names, attach symbols to named trees
- packageobjects
- load package objects
- typer
- the meat and potatoes: type the trees
- superaccessors
- add super accessors in traits and nested classes
- extmethods
- add extension methods for inline classes
- pickler
- serialize symbol tables
- refchecks
- reference/override checking, translate nested objects
- patmat
- translate match expressions
- uncurry
- uncurry, translate function values to anonymous classes
- fields
- synthesize accessors and fields, add bitmaps for lazy vals
- tailcalls
- replace tail calls by jumps
- specialize
- @specialized-driven class and method specialization
- explicitouter
- this refs to outer pointers
- erasure
- erase types, add interfaces for traits
- posterasure
- clean up erased inline classes
- lambdalift
- move nested functions to top level
- constructors
- move field definitions into constructors
- flatten
- eliminate inner classes
- mixin
- mixin composition
- cleanup
- platform-specific cleanups, generate reflective calls
- delambdafy
- remove lambdas
- jvm
- generate JVM bytecode
- terminal
- the last phase during a compilation run