Skip to content

Commit 8b3469a

Browse files
sjrdKordyjan
authored andcommitted
Spec: Apply the changes to operators.
[Cherry-picked d410376]
1 parent 3dbc430 commit 8b3469a

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

docs/_spec/01-lexical-syntax.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Scala is a line-oriented language where statements may be terminated by semi-col
164164
A newline in a Scala source text is treated as the special token “nl” if the three following criteria are satisfied:
165165

166166
1. The token immediately preceding the newline can terminate a statement.
167-
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_.
168168
1. The token appears in a region where newlines are enabled.
169169

170170
The tokens that can terminate a statement are: literals, identifiers and the following delimiters and reserved words:
@@ -185,6 +185,14 @@ with yield , . ; : = => <- <: <%
185185
A `case` token can begin a statement only if followed by a
186186
`class` or `object` token.
187187

188+
A _leading infix operator_ is a symbolic identifier such as `+`, or `approx_==`, or an identifier in backticks that:
189+
190+
- starts a new line, and
191+
- is not following a blank line, and
192+
- is followed by at least one whitespace character (including new lines) and a token that can start an expression.
193+
194+
Furthermore, if the operator appears on its own line, the next line must have at least the same indentation width as the operator.
195+
188196
Newlines are enabled in:
189197

190198
1. all of a Scala source file, except for nested regions where newlines are disabled, and

docs/_spec/03-types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ Type operators ending in a colon ‘:’ are right-associative; all other operat
172172
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.
173173
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) ...)´.
174174

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+
175177
The type operators `|` and `&` are not really special.
176178
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.
177179

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,9 @@ The scope of a type parameter includes the whole signature, including any of the
575575

576576
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.
577577

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+
578581
### Default Arguments
579582

580583
Each value parameter declaration may optionally define a default argument.

docs/_spec/05-classes-and-objects.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ LocalModifier ::= ‘abstract’
310310
| ‘sealed’
311311
| ‘implicit’
312312
| ‘lazy’
313+
| ‘infix’
313314
AccessModifier ::= (‘private’ | ‘protected’) [AccessQualifier]
314315
AccessQualifier ::= ‘[’ (id | ‘this’) ‘]’
315316
```
@@ -401,6 +402,31 @@ happen at all).
401402
Attempting to access a lazy value during its initialization might lead to looping behavior.
402403
If an exception is thrown during initialization, the value is considered uninitialized, and a later access will retry to evaluate its right hand side.
403404

405+
### `infix`
406+
The `infix` modifier applies to method definitions and type definitions.
407+
It signals that the method or type is intended for use in infix position, even if it has an alphanumeric name.
408+
409+
If a method overrides another, their `infix` annotations must agree. Either both are annotated with `infix`, or none of them are.
410+
411+
The first non-receiver parameter list of an `infix` method must define exactly one parameter. Examples:
412+
413+
```scala
414+
infix def op1(x: S): R // ok
415+
infix def op2[T](x: T)(y: S): R // ok
416+
infix def op3[T](x: T, y: S): R // error: two parameters
417+
extension (x: A)
418+
infix def op4(y: B): R // ok
419+
infix def op5(y1: B, y2: B): R // error: two parameters
420+
```
421+
422+
`infix` modifiers can also be given to type, trait or class definitions that have exactly two type parameters. An infix type like
423+
424+
```scala
425+
infix type op[X, Y]
426+
```
427+
428+
can be applied using infix syntax, i.e., `A op B`.
429+
404430
###### Example
405431
The following code illustrates the use of qualified private:
406432

@@ -1211,4 +1237,4 @@ A correctly typed version would use an _explicit_, _invariant_ type parameter `
12111237
```scala
12121238
enum View[-´T´]:
12131239
case ReflR´](f: ´R´ => ´R´) extends ViewR´]
1214-
```
1240+
```

docs/_spec/06-expressions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ This expression is then interpreted as ´e.\mathit{op}(e_1,...,e_n)´.
547547
A left-associative binary operation ´e_1;\mathit{op};e_2´ is interpreted as ´e_1.\mathit{op}(e_2)´. If ´\mathit{op}´ is right-associative and its parameter is passed by name, the same operation is interpreted as ´e_2.\mathit{op}(e_1)´.
548548
If ´\mathit{op}´ is right-associative and its parameter is passed by value, it is interpreted as `{ val ´x´=´e_1´; ´e_2´.´\mathit{op}´(´x\,´) }`, where ´x´ is a fresh name.
549549

550+
Under `-source:future`, if the method name is alphanumeric and the target method is not marked [`infix`](./05-classes-and-objects.html#infix), a deprecation warning is emitted.
551+
550552
### Assignment Operators
551553

552554
An _assignment operator_ is an operator symbol (syntax category `op` in [Identifiers](01-lexical-syntax.html#identifiers)) that ends in an equals character “`=`”, with the following exceptions:

0 commit comments

Comments
 (0)