Skip to content

private val referred to by a WeakReference should still create a field #13770

Closed
@Arthurm1

Description

@Arthurm1

In Scala 2, private variables in a class would be defined in the class. In Scala 3 they seem to be defined in the constructor.

I ran into this because I had weak references to the variables and then the variables got garbage collected - which was not what I wanted.

Compiler version

3.1.0-RC3

Minimized code

class Foo(i: Int) {
  private val bar: Int = 0
}

seems to give the equivalent in Java of...

public class Foo
{
    Foo(int i) {
        int bar = 0;
    }
}

when in Scala 2 it gave...

public class Foo
{
    private int bar = 0;

    Foo(int i) {
    }
}

I think Scala 2 also defined private[this] val in the class, not the constructor.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions