Skip to content

Conversation

KacperFKorban
Copy link
Member

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):

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("<", ", ", ">")
}

@SethTisue
Copy link
Member

SethTisue commented Aug 18, 2025

my understanding is that language.postfixOps is legacy stuff that's only still in the language as a compatibility/migration aid. so should we be increasing its power? even in Scala 2 it's been considered legacy for some years now and almost nobody uses it anymore

@KacperFKorban
Copy link
Member Author

@SethTisue I think so, but this is something that is allowed in Scala 2, so even for a legacy feature it would be nice to have feature parity. (And here the cost is non-existent).

@SethTisue
Copy link
Member

Oh, I didn't understand this just aligns with existing Scala 2 behavior. Sure, no objection then.

@KacperFKorban KacperFKorban marked this pull request as ready for review August 18, 2025 13:11
@hamzaremmal hamzaremmal enabled auto-merge (squash) August 19, 2025 06:48
@hamzaremmal hamzaremmal merged commit de18af4 into scala:main Aug 19, 2025
48 checks passed
@hamzaremmal hamzaremmal deleted the postfix-assign branch August 19, 2025 08:14
tgodzik pushed a commit to scala/scala3-lts that referenced this pull request Aug 28, 2025
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("<", ", ", ">")
}
```
[Cherry-picked de18af4]
WojciechMazur pushed a commit that referenced this pull request Sep 22, 2025
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("<", ", ", ">")
}
```
[Cherry-picked de18af4]
WojciechMazur added a commit that referenced this pull request Sep 23, 2025
…7.4 (#24032)

Backports #23775 to the 3.7.4.

PR submitted by the release tooling.
[skip ci]
@WojciechMazur WojciechMazur added this to the 3.7.4 milestone Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants