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

Reply via email to