Self Reflection

Carl Sassenrath, CTO
REBOL Technologies
26-Apr-2010 3:20 GMT

Article #0312
Main page || Index || Prior Article [0311] || Next Article [0313] || 49 Comments || Send feedback

Within a context, the word self refers to that context.

For example, within an object:

obj: object [a: 10 f: does [probe words-of self]
probe obj/f
[a f]

The self reflection shows words a and f. But, you may ask where is the word 'self?

Another example is a loop context:

repeat n 1 [probe words-of self]

where the same situation occurs.

You should know that self is a special word with an automatic binding to the context of its reference. It's indeed one of the few keywords of the REBOL language, and because self is implicit to each context, its value is not reflected in a mold of the context (as you can see above.)

Now, we must ask how flexible will we allow self usage? Should this code be allowed:

object [self: 10]

Wouldn't such usage be nonsensical? It seems so. We cannot set our context in this way, so the expression is meaningless. However, if we think of self as just another word, then the expression should be valid, shouldn't it?

In fact, one might argue that if we disallow self as a variable then we are creating a singular irregularity in the "name-space to value-space continuum". Mathematically, that sounds bad like the universe may implode, but practically speaking, who cares? Most languages are not built solely on mathematical purity, nor are purely mathematical languages all that practical in real life programming.

At the core of the issue is the fact that self must be bound to the context, but we don't want to pay that price for every context, because 99% of contexts don't refer to self at all. In R3, we solve this problem by allowing self to be a special word that has no value storage. This is an optimization where we get self almost for free.

Now... with that said, in the current version of R3, you can write:

repeat self 10 [print self]

This proves to be is an interesting situation because the context consists of two identical words: self the context and self the loop variable. It's questionable if this should be allowed, but on the other hand is such overloading really a problem? If we don't want to allow it, then would the above expression throw an error to indicate that self cannot be used in this way?


Updated 15-Apr-2024 - Edit - Copyright REBOL Technologies -