Skip to content

Commit 31d307d

Browse files
committed
Spec: Syntax of imports.
This already includes `given` imports, although the rest of contextual abstractions are not there yet.
1 parent 1fcf4fa commit 31d307d

File tree

4 files changed

+81
-54
lines changed

4 files changed

+81
-54
lines changed

docs/_spec/02-identifiers-names-and-scopes.md

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ The compiler supplies imports in a preamble to every source file.
7474
This preamble conceptually has the following form, where braces indicate nested scopes:
7575

7676
```scala
77-
import java.lang._
77+
import java.lang.*
7878
{
79-
import scala._
79+
import scala.*
8080
{
81-
import Predef._
81+
import Predef.*
8282
{ /* source */ }
8383
}
8484
}
@@ -95,8 +95,8 @@ This allows redundant type aliases to be imported without introducing an ambigui
9595
object X { type T = annotation.tailrec }
9696
object Y { type T = annotation.tailrec }
9797
object Z {
98-
import X._, Y._, annotation.{tailrec => T} // OK, all T mean tailrec
99-
@T def f: Int = { f ; 42 } // error, f is not tail recursive
98+
import X.*, Y.*, annotation.tailrec as T // OK, all T mean tailrec
99+
@T def f: Int = { f ; 42 } // error, f is not tail recursive
100100
}
101101
```
102102

@@ -107,7 +107,7 @@ Similarly, imported aliases of names introduced by package statements are allowe
107107
package p { class C }
108108

109109
// xy.scala
110-
import p._
110+
import p.*
111111
package p { class X extends C }
112112
package q { class Y extends C }
113113
```
@@ -132,27 +132,32 @@ package q {
132132
The following program illustrates different kinds of bindings and precedences between them.
133133

134134
```scala
135-
package p { // `X' bound by package clause
136-
import Console._ // `println' bound by wildcard import
137-
object Y {
138-
println(s"L4: $X") // `X' refers to `p.X' here
139-
locally {
140-
import q._ // `X' bound by wildcard import
141-
println(s"L7: $X") // `X' refers to `q.X' here
142-
import X._ // `x' and `y' bound by wildcard import
143-
println(s"L9: $x") // `x' refers to `q.X.x' here
135+
package p { // `X' bound by package clause
136+
import Console.* // `println' bound by wildcard import
137+
object Y {
138+
println(s"L4: $X") // `X' refers to `p.X' here
144139
locally {
145-
val x = 3 // `x' bound by local definition
146-
println(s"L12: $x") // `x' refers to constant `3' here
140+
import q.* // `X' bound by wildcard import
141+
println(s"L7: $X") // `X' refers to `q.X' here
142+
import X.* // `x' and `y' bound by wildcard import
143+
println(s"L9: $x") // `x' refers to `q.X.x' here
147144
locally {
148-
import q.X._ // `x' and `y' bound by wildcard import
149-
// println(s"L15: $x") // reference to `x' is ambiguous here
150-
import X.y // `y' bound by explicit import
151-
println(s"L17: $y") // `y' refers to `q.X.y' here
145+
val x = 3 // `x' bound by local definition
146+
println(s"L12: $x") // `x' refers to constant `3' here
152147
locally {
153-
val x = "abc" // `x' bound by local definition
154-
import p.X._ // `x' and `y' bound by wildcard import
155-
// println(s"L21: $y") // reference to `y' is ambiguous here
156-
println(s"L22: $x") // `x' refers to string "abc" here
157-
}}}}}}
148+
import q.X.* // `x' and `y' bound by wildcard import
149+
// println(s"L15: $x") // reference to `x' is ambiguous here
150+
import X.y // `y' bound by explicit import
151+
println(s"L17: $y") // `y' refers to `q.X.y' here
152+
locally {
153+
val x = "abc" // `x' bound by local definition
154+
import p.X.* // `x' and `y' bound by wildcard import
155+
// println(s"L21: $y") // reference to `y' is ambiguous here
156+
println(s"L22: $x") // `x' refers to string "abc" here
157+
}
158+
}
159+
}
160+
}
161+
}
162+
}
158163
```

docs/_spec/04-basic-declarations-and-definitions.md

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -692,64 +692,86 @@ completely. It is an error if the types of two alternatives ´T_i´ and
692692

693693
## Import Clauses
694694

695-
```ebnf
696-
Import ::= ‘import’ ImportExpr {‘,’ ImportExpr}
697-
ImportExpr ::= StableId ‘.’ (id | ‘_’ | ImportSelectors)
698-
ImportSelectors ::= ‘{’ {ImportSelector ‘,’}
699-
(ImportSelector | ‘_’) ‘}’
700-
ImportSelector ::= id [‘=>’ id |=>’ ‘_’]
701695
```
696+
Import ::= ‘import’ ImportExpr {‘,’ ImportExpr}
697+
ImportExpr ::= SimpleRef {‘.’ id} ‘.’ ImportSpecifier
698+
| SimpleRef `as` id
699+
ImportSpecifier ::= NamedSelector
700+
| WildcardSelector
701+
| ‘{’ ImportSelectors ‘}’
702+
NamedSelector ::= id [(‘as’ |=>’) (id | ‘_’)]
703+
WildcardSelector ::=*| ’_’ |given’ [InfixType]
704+
ImportSelectors ::= NamedSelector [‘,’ ImportSelectors]
705+
| WildCardSelector {‘,’ WildcardSelector}
706+
```
707+
708+
- In a `NamedSelector`, `=>` can only be used when inside an `ImportSelectors` and is then equivalent to `as`, to be deprecated in the future.
709+
- In a `WildcardSelector`, `_` is equivalent to `*`, to be deprecated in the future.
710+
711+
An `ImportSpecifier` that is a single `NamedSelector` or `WildcardSelector` is equivalent to an `‘{‘ ImportSelectors ‘}‘` list with that single selector.
712+
713+
An import clause with multiple import expressions `import ´p_1´.´I_1, ..., p_n´.´I_n´` is interpreted as a sequence of import clauses `import ´p_1´.´I_1´; ...; import ´p_n´.´I_n´`.
702714

703-
An import clause has the form `import ´p´.´I´` where ´p´ is a [stable identifier](03-types.html#paths) and ´I´ is an import expression.
704-
The import expression determines a set of names of importable members of ´p´ which are made available without qualification.
715+
An import clause with a single import expression has the form `import ´p´.´I´` where ´p´ is a [prefix](03-types.html#designator-types) and ´I´ is an import specifier.
716+
The import specifier determines a set of names of importable members of ´p´ which are made available without qualification as well as a set of importable `given` members which are made available in the implicit scope.
705717
A member ´m´ of ´p´ is _importable_ if it is [accessible](05-classes-and-objects.html#modifiers).
706-
The most general form of an import expression is a list of _import selectors_
718+
The most general form of an import specifier is a list of _import selectors_
707719

708720
```scala
709-
{ ´x_1´ => ´y_1, ..., x_n´ => ´y_n´, _ }
721+
{ ´x_1´ as ´y_1, ..., x_n´ as ´y_n´, *, given ´T_1´, ..., given ´T_m´, given }
710722
```
711723

712-
for ´n \geq 0´, where the final wildcard `‘_’` may be absent.
713-
It makes available each importable member `´p´.´x_i´` under the unqualified name ´y_i´. I.e. every import selector `´x_i´ => ´y_i´` renames `´p´.´x_i´` to
714-
´y_i´.
715-
If a final wildcard is present, all importable members ´z´ of ´p´ other than `´x_1, ..., x_n,y_1, ..., y_n´` are also made available under their own unqualified names.
724+
for ´n \geq 0´ and ´m \geq 0´, where the wildcards `‘*’` and `’given’` may be absent.
725+
They are decomposed into non-given selectors and given selectors.
716726

717-
Import selectors work in the same way for type and term members.
718-
For instance, an import clause `import ´p´.{´x´ => ´y\,´}` renames the term
719-
name `´p´.´x´` to the term name ´y´ and the type name `´p´.´x´` to the type name ´y´.
727+
### Non-given Imports
728+
729+
Non-given selectors make available each importable member `´p´.´x_i´` under the unqualified name ´y_i´.
730+
In other words, every import selector `´x_i´ as ´y_i´` renames `´p´.´x_i´` to ´y_i´.
731+
When `as ´y_i´` is omitted, ´y_i´ is assumed to be ´x_i´.
732+
If a final wildcard `‘*’` is present, all non-`given` importable members ´z´ of ´p´ other than `´x_1, ..., x_n, y_1, ..., y_n´` are also made available under their own unqualified names.
733+
734+
Non-given import selectors work in the same way for type and term members.
735+
For instance, an import clause `import ´p´.{´x´ => ´y´}` renames the term name `´p´.´x´` to the term name ´y´ and the type name `´p´.´x´` to the type name ´y´.
720736
At least one of these two names must reference an importable member of ´p´.
721737

722-
If the target in an import selector is a wildcard, the import selector hides access to the source member.
723-
For instance, the import selector `´x´ => _`renames” ´x´ to the wildcard symbol (which is unaccessible as a name in user programs), and thereby effectively prevents unqualified access to ´x´.
738+
If the target in an import selector is an underscore `as _`, the import selector hides access to the source member instead of importing it.
739+
For instance, the import selector `´x´ as _`renames” ´x´ to the underscore symbol (which is not accessible as a name in user programs), and thereby effectively prevents unqualified access to ´x´.
724740
This is useful if there is a final wildcard in the same import selector list, which imports all members not mentioned in previous import selectors.
725741

726-
The scope of a binding introduced by an import-clause starts immediately after the import clause and extends to the end of the enclosing block, template, package clause, or compilation unit, whichever comes first.
742+
The scope of a binding introduced by a non-given import clause starts immediately after the import clause and extends to the end of the enclosing block, template, package clause, or compilation unit, whichever comes first.
727743

728-
Several shorthands exist. An import selector may be just a simple name ´x´.
729-
In this case, ´x´ is imported without renaming, so the import selector is equivalent to `´x´ => ´x´`.
730-
Furthermore, it is possible to replace the whole import selector list by a single identifier or wildcard.
731-
The import clause `import ´p´.´x´` is equivalent to `import ´p´.{´x\,´}`, i.e. it makes available without qualification the member ´x´ of ´p´. The import clause `import ´p´._` is equivalent to `import ´p´.{_}`, i.e. it makes available without qualification all members of ´p´ (this is analogous to `import ´p´.*` in Java).
744+
### Given Imports
732745

733-
An import clause with multiple import expressions `import ´p_1´.´I_1, ..., p_n´.´I_n´` is interpreted as a sequence of import clauses `import ´p_1´.´I_1´; ...; import ´p_n´.´I_n´`.
746+
Given selectors make available in the implicit scope all the importable `given` and `implicit` members `´p´.´x´` such that `´p.x´` is a subtype of ´T_i´.
747+
A bare `given` selector without type is equivalent to `given scala.Any`.
748+
749+
The names of the given members are irrelevant for the selection, and are not made available in the normal scope of unqualified names.
734750

735751
###### Example
736752
Consider the object definition:
737753

738754
```scala
739755
object M {
740-
def z = 0, one = 1
756+
def z = 0
757+
def one = 1
741758
def add(x: Int, y: Int): Int = x + y
742759
}
743760
```
744761

745762
Then the block
746763

747764
```scala
748-
{ import M.{one, z => zero, _}; add(zero, one) }
765+
{
766+
import M.{one, z as zero, *}
767+
add(zero, one)
768+
}
749769
```
750770

751771
is equivalent to the block
752772

753773
```scala
754-
{ M.add(M.z, M.one) }
774+
{
775+
M.add(M.z, M.one)
776+
}
755777
```

0 commit comments

Comments
 (0)