You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/compiler-options/index.md
+40-22
Original file line number
Diff line number
Diff line change
@@ -25,17 +25,13 @@ title: Scala Compiler Options
25
25
26
26
## Introduction
27
27
28
-
Scala compiler `scalac` offers various **compiler options**, also referred to as **compiler flags**, to change how to compile your program.
28
+
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.
29
29
30
-
Nowadays, most people are not running `scalac` from the command line.
31
-
Instead, they use sbt, an IDE, and other tools as their interface to the compiler.
32
-
Therefore they may not even have `scalac` installed, and won't think to do `man scalac`.
30
+
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.
33
31
34
-
This page comes to the rescue for the people to find…
35
-
36
-
* What compiler options `scalac` offers
37
-
* How to use compiler options
32
+
Options may be specified on the command line to `scalac` or in the configuration of a build tool or IDE.
38
33
34
+
The Scala distribution includes a `man` page. If Scala is installed as a system command, that documentation may be available from `man scalac`.
39
35
40
36
## How to use compiler options
41
37
@@ -44,34 +40,47 @@ This page comes to the rescue for the people to find…
44
40
```bash
45
41
scalac [ <options> ] <source files>
46
42
```
43
+
Boolean flags are specified in the usual way:
44
+
45
+
`scalac -Werror -Xlint Hello.scala`
46
+
47
+
Options that require arguments use "colon" syntax:
48
+
49
+
`scalac -Vprint:parser,typer`
47
50
48
-
E.g. `scalac -encoding utf8 -Xfatal-warnings Hello.scala`
51
+
Options that take just a single argument accept traditional syntax:
49
52
50
-
Default paths can be listed by running a command line tool:
53
+
`scalac -d /tmp`
54
+
55
+
Conventionally, options have a prefix `-V` if they show "verbose" output;
56
+
`-W` to manage warnings; `-X` for extended options that modify tool behavior;
57
+
`-Y` for private options with limited support, where `Y` may suggest forking behavior.
58
+
Several options have historical aliases, such as `-Xfatal-warnings` for `-Werror`.
59
+
60
+
In Scala 2, default paths can be listed by running a tool in the distribution:
51
61
```
52
62
scala scala.tools.util.PathResolver [ <options> ]
53
63
```
54
-
55
-
64
+
That can help debug errors in options such as `--classpath`.
56
65
57
66
### Use compiler options with sbt
58
67
59
-
68
+
Here is a typical configuration of the `scalacOptions` setting in `sbt`:
60
69
61
70
```scala
62
-
scalacOptions ++=Seq(
63
-
"-encoding", "utf8", // Option and arguments on same line
64
-
"-Xfatal-warnings", // New lines for each options
65
-
"-deprecation",
66
-
"-unchecked",
71
+
scalacOptions ++=Seq( // use ++= to add to existing options
72
+
"-encoding", "utf8", // if an option takes an arg, supply it on the same line
73
+
"-feature", // then put the next option on a new line for easy editing
67
74
"-language:implicitConversions",
68
-
"-language:higherKinds",
69
75
"-language:existentials",
70
-
"-language:postfixOps"
71
-
)
76
+
"-unchecked",
77
+
"-Werror",
78
+
"-Xlint", // exploit "trailing comma" syntax so you can add an option without editing this line
79
+
) // for "trailing comma", the closing paren must be on the next line
72
80
```
81
+
The convention is always to append to the setting with `++=` and to supply one option per line.
73
82
74
-
83
+
Normally the last option will have a trailing comma so that `git diff` is a bit cleaner when options are added.
75
84
76
85
{% for category in site.data.compiler-options %}
77
86
<h2>{{ category.category }}</h2>
@@ -116,6 +125,15 @@ scalacOptions ++= Seq(
116
125
117
126
{% endfor %}
118
127
128
+
### Targeting a version of the JVM
129
+
130
+
Applications or libraries targeting the JVM may wish to specify a target version.
131
+
132
+
The `-release` option specifies the target version, such as "8" or "18".
133
+
134
+
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.
135
+
136
+
The deprecated option `-target` does not compile against the desired API, but only specifies a target class file format.
0 commit comments