Programming primitives
- obj1 _Define: obj2
Make obj1 be a shallow copy of obj2.
- obj1 _AddSlots: obj2
Add to obj1 slots like those in obj2, with the same values. Existing slots of the same name are overwritten.
- obj1 _AddSlotsIfAbsent: obj2
As _AddSlots, but do not overwrite existing slots.
- obj1 _RemoveSlot: slotName
Remove slot with name slotName
Exercise
| x <- () |
x _Define: (| a = 3 |)
x _AddSlots: (| b = 4. a = 2 |).
x _AddSlotsIfAbsent: (| a = ©x©. c = 3@4 |).
x _RemoveSlot: ©b©
|
Programming primitives
Up to now we have described the execution model in terms of ready-to-run
objects. To provide an incremental programming environment we also need
mechanism to alter the structure of an object (add and remove slots). These are the
programming primitives. It is not typical to use them from within an application,
although in some cases this makes sense (e.g., the Self parser-generator,
Mango, builds a program).
Each of these primitives has a mirror-based form (see later), which should be used
in preference to the naked primitive.
These are the most important of Self's programming primitives:
- _Define:
-
obj The receiver is redefined to be a shallow copy of the argument. All
preexisting slots in the receiver are discarded. Sometimes known as a `one-
way become'.
- _AddSlots:
-
obj Add to the receiver slots with the same names and values as in
the argument. A slot in the receiver with the same name as a slot in the
argument takes the value of the argument's slot.
- _AddSlotsIfAbsent:
-
obj Add to the receiver slots with the same names and values
as in the argument. A slot in the receiver with the same name as a slot in the
argument is not affected.
- _RemoveSlot:
-
slotName removes the slot with the name (a string) from the
receiver.
- _RemoveAllSlots
-
removes all slots from the receiver.
Exercise: What are the effects of the following statements?
| x <- () |
x _Define: (| a = 3 |)
x _AddSlots: (| b = 4. a = 2 |).
x _AddSlotsIfAbsent: (| a = 'x'. c = 3@4 |).
x _RemoveSlot: 'b'
|