groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerald Wiltse <jerrywil...@gmail.com>
Subject Re: Confirming getProperties() works differently (inside vs outside)
Date Mon, 29 Feb 2016 13:25:58 GMT
Ok, so it's a symptom of calling withTraits dynamically, now it makes
sense. That's much less common of a use case than standard trait
implementation.

While I would think a "groovy reflection" feature to capture these types of
properties would make a lot of sense, I can see this being very difficult
if not impossible, since it would require someone to override "this"
keyword, and replace it with new reflection logic.  I guess if someone
decided to try to such a feature, it would be better to use a different
keyword like thisMeta or something. The workaround from dinko is extremely
easy considering the problem. Thanks both of you.

Gerald R. Wiltse
jerrywiltse@gmail.com


On Mon, Feb 29, 2016 at 8:17 AM, Jochen Theodorou <blackdrag@gmx.org> wrote:

>
>
> On 29.02.2016 03:23, Gerald Wiltse wrote:
>
>> Is there a way for the Chameleon class to ever see that it has a
>> "lastColor" property?
>>
>> class Chameleon{
>>      String color = "green"
>>
>>      void printAllMyProperties(){
>>           this.properties.each{println it}
>>      }
>>
>> }
>>
>> trait ColorChanging {
>>      String lastColor
>>      def changeColor = {newcolor ->
>>          lastColor = this.color
>>          this.color = newcolor
>>      }
>> }
>>
>
> in case of Chameleon implements ColorChanging the answer is yes. In case of
>
> def mylizard = new Chameleon().withTraits(ColorChanging)
>>
>> mylizard.changeColor(blue)
>>
>> mylizard.printAllMyProperties()
>> mylizard.properties.each{println it}
>>
>
> ... no. Here Chameleon does not know anything about the trait and the
> trait isused like a facade around the object. For the same reason mylizard
> is an instance of ColorChanging, but not of Chameleon anymore
>
> It could probably made possible, if you change the meta class used for the
> Chameleon object you proxy with the trait. But there is no such
> implementation right now
>
> bye Jochen
>

Mime
View raw message