Groovy Parent/Child Private Field Access Weirdness With Closure

前端 未结 1 671
别跟我提以往
别跟我提以往 2021-02-10 08:12

In Groovy, I have a parent class and a child class where the parent class\'s constructor tries setting the value of a field of the parent class using a closure as in the followi

相关标签:
1条回答
  • 2021-02-10 08:31

    This is a known bug in the current version of groovy and is targeted for being fixed in groovy 2.0. See GROOVY-3073.

    It's happening because of a scoping bug in the metaclass where the closure in the first example can't see the private class level variable.

    One potential fix that gets around the issue for this situation is to declare a local alias variable in the superclass, this gets around the scoping issue in the closure. Change the constructor to this:

      ParentClass(columnCount) {
         def valueAlias = values
         columnCount.times { valueAlias.add('') }
      }
    
    0 讨论(0)
提交回复
热议问题