REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 30-Apr-2010 Edit History  

REBOL 3 Concepts: Objects: Referring to Self



Within an object! or module! the word self is pre-defined to refer to that context.

The self word makes it possible to refer to the object without knowing its name. You can use it as you would any object reference, passing it as an argument to functions, using it in paths, or returning it as the result of a function.


In the following example, the when? function will show the date field, and the dump function will display the entire contents of the object.

example: make object! [
    name: "test"
    date: now
    dump: does [probe self]
    when?: does [print self/date]

make object! [
    name: "test"
    date: 27-Apr-2010/14:45:52-7:00

Special Notes

Nested Objects

When using self within an object within an object, keep in mind that self normally refers to the innermost object.

In this code, self refers to obj2 :

obj1: make object! [
    name: "test"
    obj2: make object! [
        name: "idea"
        who?: does [probe self/name]

But, what if you need to refer to the outer object? To do so, you will need to define another field in the outer object to make the reference possible.

obj1: make object! [
    name: "test"
    myself: self
    obj2: make object! [
        name: "idea"
        who?: does [probe myself/name]


The self word is also used in modules exactly in the same way that it's used in object.

In fact, in R3 because your code is always running within a module, when used within the standard context of your code, it refers to your global context.

For example, start R3 and type:

probe words-of self
[system probe words-of]

What you're seeing here is a list of words bound to your global (user) context. As you type or run more code, you'll see those words as well. The module context automatically expands as needed.

This can be useful if you need to explicitly refer to a global variable that has the same name as a local variable. Fortunately, this situation is rare, but this is a good way to handle it.


cp: func [copy] [self/copy copy]
a: cp "test"

Here, copy is used as an argument name, so to refer to the copy function, we use the self word, which is the user module (global environment) for the program.

Non-Object Contexts

Contexts are environments where words are bound to values. Objects and modules are contexts, but so are functions and closures. However, it should be noted that R3 does not predefine the self word within these other contexts. This makes it easier for object-relative functions to refer to the self of their related objects (not to themselves as functions.)

In addition, when you use loop functions like repeat, foreach, for, and others, a special loop-context is created for the variables of the loop. The self word is not predefined here either because such loops may often be used within object-based functions, where access to the object is needed.

Changed in A98

The A98 release is an attempt to formalize and finalize the definition of the self word. Prior releases may differ.

  TOC < Back Next > - WIP Wiki Feedback Admin