Skip to content

Commit a74e720

Browse files
authored
More words and explain -release (#2364)
1 parent feb6ea7 commit a74e720

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

_overviews/compiler-options/index.md

+40-22
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,13 @@ title: Scala Compiler Options
2525

2626
## Introduction
2727

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

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

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

34+
The Scala distribution includes a `man` page. If Scala is installed as a system command, that documentation may be available from `man scalac`.
3935

4036
## How to use compiler options
4137

@@ -44,34 +40,47 @@ This page comes to the rescue for the people to find…
4440
```bash
4541
scalac [ <options> ] <source files>
4642
```
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`
4750

48-
E.g. `scalac -encoding utf8 -Xfatal-warnings Hello.scala`
51+
Options that take just a single argument accept traditional syntax:
4952

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:
5161
```
5262
scala scala.tools.util.PathResolver [ <options> ]
5363
```
54-
55-
64+
That can help debug errors in options such as `--classpath`.
5665

5766
### Use compiler options with sbt
5867

59-
68+
Here is a typical configuration of the `scalacOptions` setting in `sbt`:
6069

6170
```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
6774
"-language:implicitConversions",
68-
"-language:higherKinds",
6975
"-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
7280
```
81+
The convention is always to append to the setting with `++=` and to supply one option per line.
7382

74-
83+
Normally the last option will have a trailing comma so that `git diff` is a bit cleaner when options are added.
7584

7685
{% for category in site.data.compiler-options %}
7786
<h2>{{ category.category }}</h2>
@@ -116,6 +125,15 @@ scalacOptions ++= Seq(
116125

117126
{% endfor %}
118127

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

120138
## Additional resources
121139

0 commit comments

Comments
 (0)