groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <blackd...@gmx.org>
Subject Re: Confirming getProperties() works differently (inside vs outside)
Date Mon, 29 Feb 2016 13:17:47 GMT


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