You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/08/09 13:52:00 UTC

[jira] [Commented] (GROOVY-11142) this reference in trait fails to resolve properly

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

Eric Milles commented on GROOVY-11142:
--------------------------------------

{{proxyTarget}} can be used in place of {{this}} when using {{as}} or {{withTraits}} proxy generation.

> this reference in trait fails to resolve properly 
> --------------------------------------------------
>
>                 Key: GROOVY-11142
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11142
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 3.0.18, 4.0.13
>            Reporter: Eric Milles
>            Priority: Major
>              Labels: trait, traits
>
> Consider the following:
> {code:groovy}
> trait Foo {
>   void bar() {
>     print this
>   }
> }
> "xxx".withTraits(Foo).bar() // prints String1_groovyProxy@328cf0e1
> {code}
> {code:groovy}
> class Foo {
>   String toString() {
>     "foo"
>   }
> }
> trait Bar {
>   String getBaz() {
>     this.toString()
>   }
> }
> print(new Foo().withTraits(Bar).baz) // prints Foo2_groovyProxy@14b030a0
> {code}
> In the first example, I need to call a String method like "this.toLowerCase()" instead of "this" for it to work.  I tried "this.toStirng()" but that fails as well, as illustrated in the second example.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)