You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
scala>classFoo(private[this] vara:Int) {
| a = a +1|deffoo= a
| }
defined classFoo
scala>newFoo(0).foo
res0:Int=0
problem
I mutate a in the constructor body, so I would expect the field to be mutated, not just the constructor parameter as you can see here in the bytecode (iload_1 and istore_1 instead of getfield and putfield):
scala> :javap -c -filter Foo
Compiled from "<console>"
public class Foo {
public int foo();
Code:
0: aload_0
1: getfield #19 // Field a:I
4: ireturn
public Foo(int);
Code:
0: aload_0
1: iload_1
2: putfield #19 // Field a:I
5: aload_0
6: invokespecial #26 // Method java/lang/Object."<init>":()V
9: iload_1
10: iconst_1
11: iadd
12: istore_1
13: return
}
This is "fixed" in Scala 2.13 but it has other issues (#12002).
The rule should be, if a field needs to be created always use the field. Otherwise always use the local parameter.
The text was updated successfully, but these errors were encountered:
Jasper-M
changed the title
private[this] constructor var doesn't get updated
private[this] var field doesn't get updated in constructor body
May 15, 2020
Normally we'd close the ticket since it doesn't occur in 2.13 (and there is a separate ticket for the related problem in 2.13), but in this case I'm keeping it open and tentatively milestoning it 2.12.12 so we remember to at least consider if the (eventual) fix for #12002 is backport-able and might fix this too.
reproduction steps
using Scala 2.12.11,
problem
I mutate
a
in the constructor body, so I would expect the field to be mutated, not just the constructor parameter as you can see here in the bytecode (iload_1
andistore_1
instead ofgetfield
andputfield
):This is "fixed" in Scala 2.13 but it has other issues (#12002).
The rule should be, if a field needs to be created always use the field. Otherwise always use the local parameter.
The text was updated successfully, but these errors were encountered: