Skip to content

Conversation

@xuwei-k
Copy link
Contributor

@xuwei-k xuwei-k commented Apr 24, 2020

fix #757

maybe scala/scala#7270 ? 🤔

package example

import scala.reflect.NameTransformer

object A{
  Main.a = 1
}

object B{
  Main.b = 1
}

object Main {
  var a: Int = 0
  var b: Int = 0

  def initializeByConstructor(): Int = {
    this.a = 0
    val List(constructor) = Class.forName("example.A$").getDeclaredConstructors.toList
    constructor.setAccessible(true)
    constructor.newInstance()
    val result = Main.a
    this.a = 0
    result
  }

  def initializeByMODULE_field(): Int = {
    this.b = 0
    Class.forName("example.B$").getDeclaredField(NameTransformer.MODULE_INSTANCE_NAME).get(null)
    val result = Main.b
    this.b = 0
    result
  }

  def main(args: Array[String]): Unit = {
    println((A, B))
    println(scala.util.Properties.versionString)
    println(initializeByConstructor())
    println(initializeByMODULE_field())
  }
}
[info] version 2.12.11
[info] 1
[info] 0
[info] version 2.13.2
[info] 0
[info] 0

maybe scala/scala#7270 ?

```
package example

import scala.reflect.NameTransformer

object A{
  Main.a = 1
}

object B{
  Main.b = 1
}

object Main {
  var a: Int = 0
  var b: Int = 0

  def initializeByConstructor(): Int = {
    this.a = 0
    val List(constructor) = Class.forName("example.A$").getDeclaredConstructors.toList
    constructor.setAccessible(true)
    constructor.newInstance()
    val result = Main.a
    this.a = 0
    result
  }

  def initializeByMODULE_field(): Int = {
    this.b = 0
    Class.forName("example.B$").getDeclaredField(NameTransformer.MODULE_INSTANCE_NAME).get(null)
    val result = Main.b
    this.b = 0
    result
  }

  def main(args: Array[String]): Unit = {
    println((A, B))
    println(scala.util.Properties.versionString)
    println(initializeByConstructor())
    println(initializeByMODULE_field())
  }
}
```

```
[info] version 2.12.11
[info] 1
[info] 0
```

```
[info] version 2.13.2
[info] 0
[info] 0
```
@etorreborre etorreborre merged commit 29a8d66 into etorreborre:master Apr 26, 2020
@etorreborre
Copy link
Owner

That looks good, thanks!

@etorreborre
Copy link
Owner

@xuwei-k do you need a release with the fix?

@xuwei-k xuwei-k deleted the fix-issue-757 branch April 27, 2020 06:17
@xuwei-k
Copy link
Contributor Author

xuwei-k commented Apr 27, 2020

Yes, please 🙏

@etorreborre
Copy link
Owner

Done: https://repo1.maven.org/maven2/org/specs2/specs2-core_2.13/4.9.4/

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.

Nested mutable examples not running with Scala 2.13

2 participants