> On 06 Feb 2015, at 18:15, Marcus Denker <marcus.den...@inria.fr> wrote:
> 
> 
>> On 06 Feb 2015, at 11:50, Marcus Denker <marcus.den...@inria.fr> wrote:
>> 
>>> 
>>> Next: When building classes with Slots, the class builder need to call back 
>>> on the slot and hand the class to each. Then the slot
>>> can reflectively change the class, e.g. the PropertySlot will check if 
>>> there is already a hidden property base slot and if not, add it
>>> reflectively).
>> 
>> This is now done:
>> 
>>      
>> https://pharo.fogbugz.com/f/cases/14876/Add-call-back-in-ClassBuilder-to-call-slots-when-building-a-class
>> 
> 

So, with this mechanism in place we can actually solve a nice problem: Slot 
initialisation.

on #initialize, we want to hand the new object to each slot to give it a change 
to initialize it.
Now adding 
        self allSlots do: [ :slot | slot initialize: anObject ]

to #initialize and doing that for all object is not good as this would slot 
down all #initialize calls.
We might want to pay that later when if we happen to use it a lot, but now now.

https://pharo.fogbugz.com/f/cases/14882/Slots-add-mechanism-for-initializing-slots
 
<https://pharo.fogbugz.com/f/cases/14882/Slots-add-mechanism-for-initializing-slots>

now implements it this way:

1) Slots that want #initialize: the be called on allocation need to implement 
#wantsInitalization
2) Then magically we add #initialize if needed and, if needed, add 
        self class initializeSlots: self.
    to it.
    (this for now just uses text manipulation… later this will be a nice 
example for a concept that
    we need to reify as part of the MOP: “I want to hook into object 
initialization”).


Playing with it
——————— 
 (I need to add a tests…)

InstanceVariableSlot subclass: #InitSlot
        slots: {  }
        classVariables: {  }
        category: ‘PlayGround'

initialize: anObject
        self write: 5 to: anObject. 
        
wantsInitalization
        ^ true

Now create a class:

Object subclass: #MyCClass
        slots: { #ttt => InitSlot }
        classVariables: {  }
        category: ‘PlayGround'


MyCClass new ==> ttt is initialised with 5.


For what is this useful?
———————————

1) e.g. the virtual slot that hold the dictionary for all PropertySlots needs 
to be initialised with an empty Dict
2) We can start to experiment with Slots that specify a value in the definition
3) ….

        Marcus



Reply via email to