Domanda

I am trying to understand the self language.(Without actually coding!)

I was wondering how a method is able to modify its receivers' slot. My understanding is that: In self, 'self*' is given as parent pointer for the activation record. So that if a method is unable to find the slot locally, it will lookup "self*". But to my understanding, when in any object if I set a slot (say "x") it only sets a local slot and doesn't modify the slot "a" of its parent.

È stato utile?

Soluzione

Slots in Self, wether “local” to the receiver or “remote” as any other object, are not actually directly writable at all.

This means that even 'self' is not able to change local slots directly (without metaprogramming and mirrors, that is).

Slot Access in Self

(you can find a good discussion in the Self handbook)

However, when you create a slot on object creation time, you can decide, wether a slot is “writable”. How is that, when I said there is no writable slot?

So, you have an object:

(| a = 3. |)

then a is never writable, but when you say

(| a <- 3. |)

then there are actually two slots created: a, which contains 3 and a: which contains the assignment primitive. The assignment primitive is able to change the content of a, such that you can say

(| a <- 3 |) a: 4 " => a is 4 now"

which is a simple message send. This is not different in method activations. Any slot-changing behavior goes either

  • through methods that are assginments primitives (defined with <-),
  • or through metaprogramming and mirrors.

and hence, when your parent has an assignment slot, you surely can modify its slots:

| p . o |
p: (| a <- 3 |).
o: (|
    parent* = p.
    b <- 4.
    c = ( a: 4 ).
   |).

p a. "-> 3"
o c.
p a. "-> 4"

(If you want to do that in the shell do it like this:

bootstrap addSlotsTo: bootstrap stub -> 'globals' -> () From: (|
  p <- (| a <- 3 |).
|).
(|
  parent* = p.
  b <- 4.
  c = ( a: 4 ).
|)

then "Get It" and evaluate c)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top