|
| 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 | +``` |
0 commit comments