Skip to content

Commit db4f837

Browse files
committed
Add imports docpage
1 parent 46fcfd6 commit db4f837

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
layout: doc-page
3+
title: "Imports"
4+
---
5+
6+
The syntax of wildcard and renaming imports (and exports) has changed.
7+
8+
## Wildcard Imports
9+
10+
Wildcard imports are now expressed with `*` instead of underscore. Example:
11+
```scala
12+
import scala.annotation.* // imports everything in the annotation package
13+
```
14+
15+
If you want to import a member named `*` specifically, you can use backticks around it.
16+
17+
```scala
18+
object A:
19+
def * = ...
20+
def min = ...
21+
22+
object B:
23+
import A.`*` // imports just `*`
24+
25+
object C:
26+
import A.* // imports everything in A
27+
```
28+
29+
## Renaming Imports
30+
31+
To rename or exclude an import, we now use `as` instead of `=>`. A single renaming import no longer needs to be enclosed in braces. Examples:
32+
33+
```scala
34+
import A.{min as minimum, `*` as multiply}
35+
import Predef.{augmentString as _, *} // imports everything except augmentString
36+
import scala.annotation as ann
37+
import java as j
38+
```
39+
40+
### Migration
41+
42+
To support cross-building, Scala 3.0 supports the old import syntax with `_` for wildcards and `=>` for renamings in addition to the new one. The old syntax
43+
will be dropped in a future versions. Automatic rewritings from old to new syntax
44+
are offered under settings `-source 3.1-migration -rewrite`.
45+
46+
### Syntax
47+
48+
```
49+
Import ::= ‘import’ ImportExpr {‘,’ ImportExpr}
50+
Export ::= ‘export’ ImportExpr {‘,’ ImportExpr}
51+
ImportExpr ::= SimpleRef {‘.’ id} ‘.’ ImportSpec
52+
ImportSpec ::= NamedSelector
53+
| WildcardSelector
54+
| ‘{’ ImportSelectors) ‘}’
55+
NamedSelector ::= id [‘as’ (id | ‘_’)]
56+
WildCardSelector ::= ‘*' | ‘given’ [InfixType]
57+
ImportSelectors ::= NamedSelector [‘,’ ImportSelectors]
58+
| WildCardSelector {‘,’ WildCardSelector}
59+
```

docs/docs/reference/changed-features/main-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ For instance, the `happyBirthDay` method above would generate additional code eq
6262

6363
```scala
6464
final class happyBirthday:
65-
import scala.util.CommandLineParser => CLP
65+
import scala.util.CommandLineParser as CLP
6666
<static> def main(args: Array[String]): Unit =
6767
try
6868
happyBirthday(

docs/sidebar.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ sidebar:
115115
url: docs/reference/other-new-features/control-syntax.html
116116
- title: Optional Braces
117117
url: docs/reference/other-new-features/indentation.html
118-
- title: Imports
119-
url: docs/reference/other-new-features/imports.html
120118
- title: Explicit Nulls
121119
url: docs/reference/other-new-features/explicit-nulls.html
122120
- title: Safe Initialization
@@ -131,6 +129,8 @@ sidebar:
131129
url: docs/reference/changed-features/operators.html
132130
- title: Wildcard Types
133131
url: docs/reference/changed-features/wildcards.html
132+
- title: Imports
133+
url: docs/reference/changed-features/imports.html
134134
- title: Type Checking
135135
url: docs/reference/changed-features/type-checking.html
136136
- title: Type Inference

0 commit comments

Comments
 (0)