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: docs/_spec/01-lexical-syntax.md
+35-6Lines changed: 35 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -27,8 +27,6 @@ The principle of optional braces is that any keyword that can be followed by `{`
27
27
28
28
The lexical analyzer inserts `indent` and `outdent` tokens that represent regions of indented code [at certain points](./other-new-features/indentation.md).
29
29
30
-
´\color{red}{\text{TODO SCALA3: Port soft-modifier.md and link it here.}}´
31
-
32
30
In the context-free productions below we use the notation `<<< ts >>>` to indicate a token sequence `ts` that is either enclosed in a pair of braces `{ ts }` or that constitutes an indented region `indent ts outdent`.
33
31
Analogously, the notation `:<<< ts >>>` indicates a token sequence `ts` that is either enclosed in a pair of braces `{ ts }` or that constitutes an indented region `indent ts outdent` that follows a `colon` token.
34
32
@@ -121,12 +119,35 @@ type val var while with yield
121
119
122
120
Additionally, the following soft keywords are reserved only in some situations.
123
121
124
-
´\color{red}{\text{TODO SCALA3: Port soft-modifier.md and link it here.}}´
125
-
126
122
```
127
-
as derives end extension infix inline opaque open transparent using | * + -
123
+
as derives end extension infix inline opaque
124
+
open transparent using
125
+
| * + -
128
126
```
129
127
128
+
A soft modifier is one of the identifiers `infix`, `inline`, `opaque`, `open` and `transparent`.
129
+
130
+
A soft keyword is a soft modifier, or one of `as`, `derives`, `end`, `extension`, `using`, `|`, `+`, `-`, `*`.
131
+
132
+
A soft modifier is treated as an actual modifier of a definition if it is followed by a hard modifier or a keyword combination starting a definition (`def`, `val`, `var`, `type`, `given`, `class`, `trait`, `object`, `enum`, `case class`, `case object`).
133
+
Between the two words, there may be a sequence of newline tokens and/or other soft modifiers.
134
+
135
+
Otherwise, soft keywords are treated as actual keywords in the following situations:
136
+
137
+
-`as`, if it appears in a renaming import clause.
138
+
-`derives`, if it appears after an extension clause or after the name and possibly parameters of a class, trait, object, or enum definition.
139
+
-`end`, if it appears at the start of a line following a statement (i.e. definition or toplevel expression) and is followed on the same line by a single non-comment token that is:
140
+
- one of the keywords `for`, `given`, `if`, `match`, `new`, `this`, `throw`, `try`, `val`, `while`, or
141
+
- an identifier.
142
+
-`extension`, if it appears at the start of a statement and is followed by `(` or `[`.
143
+
-`inline`, if it is followed by any token that can start an expression.
144
+
-`using`, if it appears at the start of a parameter or argument list.
145
+
-`|`, if it separates two patterns in an alternative.
146
+
-`+`, `-`, if they appear in front of a type parameter.
147
+
-`*`, if it appears in a wildcard import, or if it follows the type of a parameter, or if it appears in a vararg splice `x*`.
148
+
149
+
Everywhere else, a soft keyword is treated as a normal identifier.
150
+
130
151
<!---->
131
152
132
153
> When one needs to access Java identifiers that are reserved words in Scala, use backquote-enclosed strings.
@@ -143,7 +164,7 @@ Scala is a line-oriented language where statements may be terminated by semi-col
143
164
A newline in a Scala source text is treated as the special token “nl” if the three following criteria are satisfied:
144
165
145
166
1. The token immediately preceding the newline can terminate a statement.
146
-
1. The token immediately following the newline can begin a statement.
167
+
1. The token immediately following the newline can begin a statement and is not a _leading infix operator_.
147
168
1. The token appears in a region where newlines are enabled.
148
169
149
170
The tokens that can terminate a statement are: literals, identifiers and the following delimiters and reserved words:
Copy file name to clipboardExpand all lines: docs/_spec/03-types.md
+5Lines changed: 5 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -172,6 +172,8 @@ Type operators ending in a colon ‘:’ are right-associative; all other operat
172
172
In a sequence of consecutive type infix operations ´t_0 \, \mathit{op} \, t_1 \, \mathit{op_2} \, ... \, \mathit{op_n} \, t_n´, all operators ´\mathit{op}\_1, ..., \mathit{op}\_n´ must have the same associativity.
173
173
If they are all left-associative, the sequence is interpreted as ´(... (t_0 \mathit{op_1} t_1) \mathit{op_2} ...) \mathit{op_n} t_n´, otherwise it is interpreted as ´t_0 \mathit{op_1} (t_1 \mathit{op_2} ( ... \mathit{op_n} t_n) ...)´.
174
174
175
+
Under `-source:future`, if the type name is alphanumeric and the target type is not marked [`infix`](./05-classes-and-objects.html#infix), a deprecation warning is emitted.
176
+
175
177
The type operators `|` and `&` are not really special.
176
178
Nevertheless, unless shadowed, they resolve to [the fundamental type aliases `scala.|` and `scala.&`](./12-the-scala-standard-library.html#fundamental-type-aliases), which represent [union and intersection types](#union-and-intersection-types), respectively.
177
179
@@ -493,6 +495,9 @@ All parameterized class types are value types.
493
495
In the concrete syntax of wildcard type arguments, if both bounds are omitted, the real bounds are inferred from the bounds of the corresponding type parameter in the target type constructor (which must be concrete).
494
496
If only one bound is omitted, `Nothing` or `Any` is used, as usual.
495
497
498
+
Also in the concrete syntax, `_` can be used instead of `?` for compatibility reasons, with the same meaning.
499
+
This alternative will be deprecated in the future, and is already deprecated under `-source:future`.
500
+
496
501
#### Simplification Rules
497
502
498
503
Wildcard type arguments used in covariant or contravariant positions can always be simplified to regular types.
Copy file name to clipboardExpand all lines: docs/_spec/04-basic-declarations-and-definitions.md
+57-29Lines changed: 57 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -164,7 +164,7 @@ A variable definition `var ´p´ = ´e´` where ´p´ is a pattern other than a
164
164
165
165
The name of any declared or defined variable may not end in `_=`.
166
166
167
-
A variable definition `var ´x´: ´T´ = _` can appear only as a member of a template.
167
+
The right-hand-side of a mutable variable definition that is a member of a template can be the special reference `scala.compiletime.uninitialized`: `var ´x´: ´T´ = scala.compiletime.uninitialized`.
168
168
It introduces a mutable field with type ´T´ and a default initial value.
169
169
The default value depends on the type ´T´ as follows:
170
170
@@ -178,6 +178,9 @@ The default value depends on the type ´T´ as follows:
178
178
|`()`|`Unit`|
179
179
|`null`| all other types |
180
180
181
+
`scala.compiletime.uninitialized` can never appear anywhere else.
182
+
For compatibility with Scala 2, the syntax `var ´x´: ´T´ = _` is accepted as equivalent to using `uninitialized`.
183
+
181
184
When they occur as members of a template, both forms of variable definition also introduce a getter method ´x´ which returns the value currently assigned to the variable, as well as a setter method `´x´_=` which changes the value currently assigned to the variable.
182
185
The methods have the same signatures as for a variable declaration.
183
186
The template then has these getter and setter methods as members, whereas the original variable cannot be accessed directly as a template member.
@@ -572,6 +575,9 @@ The scope of a type parameter includes the whole signature, including any of the
572
575
573
576
A _value parameter clause_ ´\mathit{ps}´ consists of zero or more formal parameter bindings such as `´x´: ´T´` or `´x: T = e´`, which bind value parameters and associate them with their types.
574
577
578
+
A unary operator must not have explicit parameter lists even if they are empty.
579
+
A unary operator is a method named `"unary_´op´"` where ´op´ is one of `+`, `-`, `!`, or `~`.
580
+
575
581
###DefaultArguments
576
582
577
583
Each value parameter declaration may optionally define a default argument.
@@ -729,64 +735,86 @@ completely. It is an error if the types of two alternatives ´T_i´ and
729
735
730
736
##ImportClauses
731
737
732
-
```ebnf
733
-
Import::= ‘import’ ImportExpr {‘,’ ImportExpr}
734
-
ImportExpr::=StableId ‘.’ (id | ‘_’ |ImportSelectors)
-In a `NamedSelector`, `=>` can only be used when inside an `ImportSelectors` and is then equivalent to `as`, to be deprecated in the future.
752
+
-In a `WildcardSelector`, `_` is equivalent to `*`, to be deprecated in the future.
753
+
754
+
An `ImportSpecifier` that is a single `NamedSelector` or `WildcardSelector` is equivalent to an `‘{‘ ImportSelectors ‘}‘` list with that single selector.
755
+
756
+
Animportclausewithmultipleimportexpressions`import ´p_1´.´I_1, ..., p_n´.´I_n´`isinterpreted as asequenceofimportclauses`import ´p_1´.´I_1´; ...; import ´p_n´.´I_n´`.
739
757
740
-
Animportclausehastheform`import ´p´.´I´`where ´p´ isa [stableidentifier](03-types.html#paths) and ´I´ isanimportexpression.
Animportclausewithasingleimportexpressionhastheform`import ´p´.´I´`where ´p´ isa [prefix](03-types.html#designator-types) and ´I´ isanimportspecifier.
759
+
Theimportspecifierdeterminesasetofnamesofimportablemembersof ´p´ whicharemadeavailablewithoutqualification as well as asetofimportable`given`memberswhicharemadeavailableintheimplicitscope.
742
760
A member ´m´ of ´p´ is _importable_ if it is [accessible](05-classes-and-objects.html#modifiers).
743
-
The most general form of an importexpressionisalistof_importselectors_
761
+
The most general form of an importspecifierisalistof_importselectors_
744
762
745
763
```scala
746
-
{ ´x_1´ => ´y_1, ..., x_n´ => ´y_n´, _ }
764
+
{ ´x_1´ as ´y_1, ..., x_n´ as ´y_n´, *, given ´T_1´, ..., given ´T_m´, given }
747
765
```
748
766
749
-
for ´n \geq 0´, where the final wildcard `‘_’` may be absent.
750
-
It makes available each importable member `´p´.´x_i´` under the unqualified name ´y_i´. I.e. every importselector`´x_i´ => ´y_i´`renames`´p´.´x_i´`to
751
-
´y_i´.
752
-
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.
767
+
for ´n \geq 0´ and ´m \geq 0´, where the wildcards `‘*’` and `’given’` may be absent.
768
+
They are decomposed into non-givenselectors and givenselectors.
769
+
770
+
###Non-givenImports
753
771
754
-
Import selectors work in the same way fortypeand term members.
755
-
For instance, an importclause`import ´p´.{´x´ => ´y\,´}`renamestheterm
756
-
name `´p´.´x´` to the term name ´y´ and the typename `´p´.´x´` to the typename ´y´.
772
+
Non-givenselectors make available each importable member `´p´.´x_i´` under the unqualified name ´y_i´.
773
+
In other words, every importselector`´x_i´ as ´y_i´`renames`´p´.´x_i´`to ´y_i´.
774
+
When `as ´y_i´` is omitted, ´y_i´ is assumed to be ´x_i´.
775
+
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.
776
+
777
+
Non-givenimport selectors work in the same way fortypeand term members.
778
+
For instance, an importclause`import ´p´.´x´ as ´y´`renamesthetermname`´p´.´x´`tothetermname ´y´ andthetypename`´p´.´x´`tothetypename ´y´.
757
779
At least one of these two names must reference an importable member of ´p´.
758
780
759
-
If the target in an importselectorisawildcard, theimportselectorhidesaccesstothesourcemember.
760
-
For instance, the importselector`´x´ => _` “renames” ´x´ tothewildcardsymbol (whichisunaccessible as anameinuserprograms), andtherebyeffectivelypreventsunqualifiedaccessto ´x´.
781
+
If the target in an importselectorisanunderscore`as _`, theimportselectorhidesaccesstothesourcememberinsteadofimportingit.
782
+
For instance, the importselector`´x´ as _` “renames” ´x´ totheunderscoresymbol (whichisnotaccessible as anameinuserprograms), andtherebyeffectivelypreventsunqualifiedaccessto ´x´.
761
783
This is useful if there is a final wildcard in the same importselectorlist, whichimportsallmembersnotmentionedinpreviousimportselectors.
762
784
763
-
The scope of a binding introduced by an import-clause starts immediately after the importclauseandextendstotheendoftheenclosingblock, template, packageclause, orcompilationunit, whichevercomesfirst.
785
+
The scope of a binding introduced by a non-givenimportclause starts immediately after the importclauseandextendstotheendoftheenclosingblock, template, packageclause, orcompilationunit, whichevercomesfirst.
764
786
765
-
Several shorthands exist. Animportselectormaybejustasimplename ´x´.
766
-
Inthiscase, ´x´ is imported without renaming, so the importselectorisequivalentto`´x´ => ´x´`.
767
-
Furthermore, it is possible to replace the whole importselectorlistbyasingleidentifierorwildcard.
768
-
Theimportclause`import ´p´.´x´`isequivalentto`import ´p´.{´x\,´}`, i.e. itmakesavailablewithoutqualificationthemember ´x´ of ´p´. Theimportclause`import ´p´._`isequivalentto`import ´p´.{_}`, i.e. itmakesavailablewithoutqualificationallmembersof ´p´ (thisisanalogousto`import ´p´.*`inJava).
787
+
###GivenImports
769
788
770
-
Animportclausewithmultipleimportexpressions`import ´p_1´.´I_1, ..., p_n´.´I_n´`isinterpreted as asequenceofimportclauses`import ´p_1´.´I_1´; ...; import ´p_n´.´I_n´`.
789
+
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´.
790
+
A bare `given` selector without typeis equivalent to `given scala.Any`.
791
+
792
+
The names of the givenmembers are irrelevant for the selection, and are not made available in the normal scope of unqualified names.
0 commit comments