Skip to content

Commit de18af4

Browse files
fix: allow postfix setters under language.postfixOps (#23775)
Allow for postfix operators to be followed by assigns. This enables the definition and use of the following syntax (more precisely the parsing of the `>_=` method as a `postfix operator + assign`): ```scala val v = new Vector(1, 2, 3) println(v) // prints <1, 2, 3> v<1> = 10 // assign 10 to element at index 1 println(v) // prints <1, 10, 3> println(v<1>) // prints: value at 1 is 10 // Definition of Vector: class Vector(values: Int*) { val data = values.toArray class Getter(i: Int) { def `>_=`(x: Int) = data(i) = x def > : Int = data(i) } def < (i:Int) = new Getter(i) override def toString = data.mkString("<", ", ", ">") } ```
1 parent aaa39c5 commit de18af4

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,7 @@ object Parsers {
23732373
* | ForExpr
23742374
* | [SimpleExpr `.'] id `=' Expr
23752375
* | PrefixOperator SimpleExpr `=' Expr
2376+
* | InfixExpr id [nl] `=' Expr -- only if language.postfixOps is enabled
23762377
* | SimpleExpr1 ArgumentExprs `=' Expr
23772378
* | PostfixExpr [Ascription]
23782379
* | ‘inline’ InfixExpr MatchClause
@@ -2528,7 +2529,7 @@ object Parsers {
25282529
def expr1Rest(t: Tree, location: Location): Tree =
25292530
if in.token == EQUALS then
25302531
t match
2531-
case Ident(_) | Select(_, _) | Apply(_, _) | PrefixOp(_, _) =>
2532+
case Ident(_) | Select(_, _) | Apply(_, _) | PrefixOp(_, _) | PostfixOp(_, _) =>
25322533
atSpan(startOffset(t), in.skipToken()) {
25332534
val loc = if location.inArgs then location else Location.ElseWhere
25342535
Assign(t, subPart(() => expr(loc)))

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
261261
| ForExpr
262262
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
263263
| PrefixOperator SimpleExpr ‘=’ Expr Assign(expr, expr)
264+
| InfixExpr id [nl] `=' Expr Assign(expr, expr) -- only if language.postfixOps is enabled
264265
| SimpleExpr ArgumentExprs ‘=’ Expr Assign(expr, expr)
265266
| PostfixExpr [Ascription]
266267
| ‘inline’ InfixExpr MatchClause

docs/_docs/reference/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[
246246
| ForExpr
247247
| [SimpleExpr ‘.’] id ‘=’ Expr
248248
| PrefixOperator SimpleExpr ‘=’ Expr
249+
| InfixExpr id [nl] `=' Expr -- only if language.postfixOps is enabled
249250
| SimpleExpr ArgumentExprs ‘=’ Expr
250251
| PostfixExpr [Ascription]
251252
| ‘inline’ InfixExpr MatchClause

tests/pos/vec-access-syntax.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.language.postfixOps
2+
3+
class Vector(values: Int*) {
4+
val data = values.toArray
5+
class Getter(i: Int) {
6+
def `>_=`(x: Int) =
7+
data(i) = x
8+
def > : Int =
9+
data(i)
10+
}
11+
def < (i:Int) = new Getter(i)
12+
override def toString = data.mkString("<", ", ", ">")
13+
}
14+
15+
object Test {
16+
def main(args: Array[String]): Unit = {
17+
val v = new Vector(1, 2, 3)
18+
println(v) // prints <1, 2, 3>
19+
v<1> = 10 // assign 10 to element at index 1
20+
println(v) // prints <1, 10, 3>
21+
println(v<1>) // prints: value at 1 is 10
22+
}
23+
}

0 commit comments

Comments
 (0)