:trickle-down-slots
<-Previous | ^UP^ | Next->
Recall in the Getting Started with GendL tutorial, in the Defining Objects topic we discussed how values may be passed from a parent object to a child. The following code summarises the process discussed
(define-object top-level-1 (base-object):computed-slots((my-slot-1 5)):objects((child-1 :type 'child-1:my-slot-1 (the my-slot-1))(child-2 :type 'child-2)))(define-object child-1 (base-object):input-slots(my-slot-1))(define-object child-2 (base-object):input-slots((my-slot-1 3)))
GDL-USER> (make-self 'top-level-1)
#<TOP-LEVEL-1 #x210522168D>GDL-USER> (the child-1 my-slot-1)
5GDL-USER> (the child-2 my-slot-1)
3
Furthermore, we covered the use of the :pass-down input to a child object in the Wall Example topic
(define-object top-level-2 (base-object):computed-slots((my-slot-1 5)):objects((child-1 :type 'child-1:pass-down (my-slot-1))))
GDL-USER> (make-self 'top-level-2)
#<TOP-LEVEL-1 #x210561B13D>GDL-USER> (the child-1 my-slot-1)
5
There is, however, a further way to ensure values flow through from a parent to a child - by using :trickle-down-slots
When a slot is defined as a :trickle-down-slot, it will automatically flow through to a child object providing both of the following conditions are satisfied
- The child object has an :input-slot of the same name
- And that :input-slot in the child object is tagged as :defaulting and given a default value
(define-object top-level-3 (base-object):computed-slots((my-slot-2 2)):trickle-down-slots(my-slot-2):objects((child-3 :type 'child-3)(child-4 :type 'child-4)))(define-object child-3 (base-object):input-slots ((my-slot-2 3 :defaulting)))(define-object child-4 (base-object):input-slots ((my-slot-2 5)))
GDL-USER> (make-self 'top-level-3)
#<TOP-LEVEL-3 #x2104A23E2D>GDL-USER> (the my-slot-2)
2GDL-USER> (the child-3 my-slot-2)
2GDL-USER> (the child-4 my-slot-2)
5
Even if the slot is defined in the parent as a :trickle-down-slot, if an input is given explicitly it will be used, over-riding the :trickle-down-slot value
(define-object top-level-4 (base-object):computed-slots((my-slot-3 2)):trickle-down-slots(my-slot-3):objects((child-5 :type 'child-5:my-slot-3 1)))(define-object child-5 (base-object):input-slots((my-slot-3 5 :defaulting)))
GDL-USER> (make-self 'top-level-4)
#<TOP-LEVEL-4 #x2104A3B93D>GDL-USER> (the child-5 my-slot-3)
1
:trickle-down-slots are a very convenient and semi-automatic way to pass values form parent to child. An example use is with the box object, where length, width and height are all :trickle-down-slots. In previous examples, we have explicitly passed the values through, however strictly speaking this has not been required. One word of caution; it can sometimes be difficult to see where values are coming from if :trickle-down-slots are used extensively and particularly if they are incorporated into mixins, but as with all things used appropriately they are a useful addition to the developer toolbox
Resources
trickle-down-slots.lisp |