A Traits question

itsme213 itsme213 at hotmail.com
Fri Feb 15 15:00:11 UTC 2008


"Damien Cassou" <damien.cassou at gmail.com> wrote in message
>I do not understand what you are trying to achieve.

Say I have a trait which manipulates a collection inst-var:

Trait named #TCollectionInstVar
  >> add: x
            self iVar add: x
  >> remove: x
            self iVar remove: x
  >> do: aBlock
            self iVar do: aBlock

I have a class which could use this trait on two of its instance-variables:

Object subclass: Person
   instanceVariables: 'cars houses'
Person>>cars
   ^ cars ifNil: [cars := OrderedCollection new]
Person>>houses
   ^ houses ifNil: [houses := OrderedCollection new]

I want to use TCollectionInstVar on my Person class, twice, on #houses and 
#cars. I need to rename the required methods to do this:
    TCollectionInstVar @ {
            #add -> #addCar.
            #remove -> #removeCar.
            #do -> #carsDo  }
        requiring: { #iVar -> #cars }
and
    TCollectionInstVar @ {
            #add -> #addHouse.
            #remove -> #removeHouse.
            #do -> #housesDo }
        requiring: { #iVar -> #houses }

This would be equivalent to using two (anonymous, auto-generated) traits 
which are derived from TCollectionInstVar, which rename the required method 
from #iVar to #cars and #houses, respectively (i.e. any send of "self iVar" 
is rewritten to a "self cars" or "self houses").

Hope that example helps.

Thanks - Sophie






More information about the Squeak-dev mailing list