neo1949 opened a new pull request #888: Fix assertion error for sample code
URL: https://github.com/apache/groovy/pull/888
 
 
   The original sample code in [3.2.4. Delegation 
strategy](http://www.groovy-lang.org/closures.html#_delegation_strategy_2) 
assert failed:
   ```groovy
   class Person {
       String name
   }
   def p = new Person(name:'Igor')
   def cl = { name.toUpperCase() }                 
   cl.delegate = p                                 
   assert cl() == 'IGOR'   
   ```
   
   My sample code:
   ```groovy
   class Person {
       String name
   }
   
   class GroovyClosure {
       static void main(args) {
           def p = new Person(name: 'Igor')
           def cl = { name.toUpperCase() }          // 1
           cl.delegate = p
           assert cl() == 'IGOR'
       }
   }
   ```
   
   And the running result is like this:
   ```
   Exception in thread "main" Assertion failed: 
   
   assert cl() == 'IGOR'
          |    |
          |    false
          GROOVYCLOSURE
   
        at 
org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:404)
        at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)
        at GroovyClosure.main(GroovyClosure.groovy:10)
   ```
   
   The assertion will not fail if adding a `delegate` property in closure `cl`:
   ```groovy
   def cl = { delegate.name.toUpperCase() }
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to