-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport "Make more anonymous functions static" to LTS (#20799)
Backports #19251 to the LTS branch. PR submitted by the release tooling. [skip ci]
- Loading branch information
Showing
2 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// scalajs: --skip | ||
|
||
object Test extends App { | ||
val field = 1 | ||
def x(): Int => String = (i: Int) => i.toString | ||
def y(): () => String = () => field.toString | ||
|
||
locally { | ||
assert(x() == x()) // true on Scala 2, was false on Scala 3... | ||
assert(y() == y()) // also true if `y` accesses object-local fields | ||
|
||
def z(): Int => String = (i: Int) => i.toString | ||
assert(z() != z()) // lambdas in constructor are not lifted to static, so no memoization (Scala 2 lifts them, though). | ||
} | ||
|
||
val t1 = new C | ||
val t2 = new C | ||
|
||
locally { | ||
assert(t1.x() == t2.x()) // true on Scala 2, was false on Scala 3... | ||
} | ||
} | ||
class C { | ||
def x(): Int => String = (i: Int) => i.toString | ||
} |