[ 
https://issues.apache.org/jira/browse/GROOVY-8820?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16659149#comment-16659149
 ] 

Frederico Costa Galvão commented on GROOVY-8820:
------------------------------------------------

Can be reproduced with as little code as:

 
{code:groovy}
trait Bacon {
    def what() {
        String[] l = []
        l.with {
            length
        }
    }
}

class B implements Bacon {}

new B().what()
{code}

Changing `trait` with `class` and `implements` with `extends` makes the code 
run properly.

 

 

 

> Closure inside trait cannot use Closure's delegate variables
> ------------------------------------------------------------
>
>                 Key: GROOVY-8820
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8820
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.15, 3.0.0-alpha-3, 2.5.2
>         Environment: Groovy Version: 3.0.0-alpha-3 JVM: 1.8.0_171 Vendor: 
> Oracle Corporation OS: Mac OS X
>            Reporter: Renato Athaydes
>            Priority: Major
>
> The following code throws an error at runtime:
>  
> {code:java}
> trait T {
>     def doIt() {
>         return {
>             n = 1
>         }
>     }
> }
> class Delegate {
>     int n
> }
>     
> class Tish implements T {
>     def go() {
>         def closure = doIt()
>         def d = new Delegate()
>         closure.delegate = d
>         closure()
>         assert d.n == 1
>         println "All good!"
>     }
> }
> new Tish().go(){code}
>  
> This is the error I see on Groovy 3.0.0-alpha-3 (same as previous versions):
>  
> {code:java}
> groovy.lang.MissingPropertyException: No such property: n for class: 
> Tish{code}
>  
> This is expected to work because, outside of traits, it does: this runs 
> successfully:
>  
> {code:java}
> class T {
>     def doIt() {
>         return {
>             n = 1
>         }
>     }
> }
> class Delegate {
>     int n
> }
>     
> class Tisha extends T {
>     def go() {
>         def closure = doIt()
>         def d = new Delegate()
>         closure.delegate = d
>         closure()
>         assert d.n == 1
>         println "All good!"
>     }
> }
> new Tisha().go(){code}
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to