Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.5, 1.8-beta-2
-
None
-
None
Description
In the following code:
@Category(Integer) class MyOps { def multiplesUpTo4() { [this * 2, this * 3, this * 4] } def multiplesUpTo(num) { (2..num).collect{ i -> this * i } } def alsoMultiplesUpTo(num) { def ans = (2..num).collect{ i -> this * i } ans } // def twice() { // def (twice, thrice, quad) = multiplesUpTo4() // twice // } } use(MyOps) { println 21.multiplesUpTo4() // => [42, 63, 84] println 21.multiplesUpTo(6) // => [42, 63, 84, 105, 126] // println 21.alsoMultiplesUpTo(6) // (A) // println 21.twice() // (B) }
If the line marked (A) is uncommented, then it fails at runtime with
groovy.lang.MissingPropertyException: No such property: i for class: java.lang.Integer
Looking at the transformed code for alsoMultiplesUpTo as per the AST browser, it is easy to see why we get that error:
java.lang.Object ans = (2.. num).collect({ java.lang.Object i -> $this * $this.i })
Meanwhile the code for the nearly identical earlier method multiplesUpTo is as expected:
(2.. num).collect({ java.lang.Object i -> $this * i })
If the "twice" method is uncommented, then we get a ClassCastException at compile time.
Also, if you create the category long hand (i.e. not using the AST transform) both problems go away.