Skip to content

Commit e7f51f8

Browse files
committed
Document syntax change
1 parent 2abbb45 commit e7f51f8

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

docs/docs/reference/changed-features/operators.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ layout: doc-page
33
title: Rules for Operators
44
---
55

6-
There are two annotations that regulate operators: `infix` and `alpha`.
6+
The rules for infix operators have changed. There are two annotations that regulate operators: `infix` and `alpha`.
7+
Furthermore, a syntax change allows infix operators to be written on the left in a multi-line expression.
78

89
## The @alpha Annotation
910

@@ -127,3 +128,53 @@ The purpose of the `@infix` annotation is to achieve consistency across a code b
127128

128129
5. To smooth migration to Scala 3.0, alphanumeric operations will only be deprecated from Scala 3.1 onwards,
129130
or if the `-strict` option is given in Dotty/Scala 3.
131+
132+
## Syntax Change
133+
134+
Infix operators can now appear at the start of lines in a multi-line expression. Examples:
135+
```scala
136+
val str = "hello"
137+
++ " world"
138+
++ "!"
139+
140+
def condition =
141+
x > 0
142+
|| xs.exists(_ > 0)
143+
|| xs.isEmpty
144+
```
145+
Previously, these expressions would have been rejected, since the compiler's semicolon inference
146+
would have treated the continuations `++ " world"` or `|| xs.isEmpty` as separate statements.
147+
148+
To make this syntax work, the rules are modified to not infer semicolons in front of leading infix operators.
149+
A _leading infix operator_ is
150+
- a symbolic identifier such as `+`, or `approx_==`, or an identifier in backticks,
151+
- that starts a new line,
152+
- that precedes a token on the same line that can start an expression,
153+
- and that is immediately followed by at least one space character `' '`.
154+
155+
Example:
156+
157+
```scala
158+
freezing
159+
| boiling
160+
```
161+
This is recognized as a single infix operation. Compare with:
162+
```scala
163+
freezing
164+
!boiling
165+
```
166+
This is seen as two statements, `freezing` and `!boiling`. The difference is that only the operator in the first example
167+
is followed by a space.
168+
169+
Another example:
170+
```scala
171+
println("hello")
172+
???
173+
??? match { case 0 => 1 }
174+
```
175+
This code is recognized as three different statements. `???` is syntactically a symbolic identifier, but
176+
neither of its occurrences is followed by a space and a token that can start an expression.
177+
178+
179+
180+

0 commit comments

Comments
 (0)