Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
2.3.0-beta-1
-
None
Description
trait Level { int maxLevel int currentLevel = 0 void foo() { if( currentLevel < maxLevel ) { currentLevel += 1 } } } class Leveller implements Level { Leveller() { maxLevel = 3 } } def v = new Leveller() v.foo() v.foo() v.foo() v.foo() assert v.currentLevel == 3
fails with
Assertion failed: assert v.currentLevel == 3 | | | | 0 false Leveller@1f757867
But if you change the trait to:
trait Level { int maxLevel int currentLevel = 0 void foo() { if( Level__currentLevel < maxLevel ) { Level__currentLevel += 1 } } }
It works...
I'm guessing this is a bug, as the given example (http://beta.groovy-lang.org/docs/groovy-2.3.0-beta-1/html/documentation/core-traits.html#_private_fields) of:
trait Counter { private int count = 0 int count() { ++count } } class Foo implements Counter {} def f = new Foo() assert f.count() == 1
doesn't work if you add:
assert f.count() == 2
to the bottom