[REBOL] Words, Bindings and Contexts. (4)
From: lmecir:geocities at: 24-Jul-2000 2:20
Note: The last two sections (Context Hierarchy. Pardon?; "Context
hierarchy"? Aye, aye, sir!) were complicated and not very useful,
but they can be skipped.
What are Rebol Contexts?
*****************************
Rebol Contexts are existing data, sometimes available to the user.
They are used internally by Rebol for the purposes of many Core
functions. Their main purpose is to serve as attribute of Rebol
Words - ie. Rebol Context is a part of any Word's Binding - in
different wording: every Rebol Word knows its Context. Proof: see
the function Same-context? defined in part 3.
Known Rebol Contexts: Available directly as:
a) Global Context Rebol/Words
b) Objects themselves
c) Function Contexts not available
d) Use Contexts not available
e) Special Contexts not available
Properties:
a) Global Context is the Default Context - Words are normally
Global Context Words, if not stated otherwise.
b) Objects are Local Contexts - as opposed to Global Context. They
are created by Make native. The list of Words object O
contains
- ie. the list of Words that are local to O is
available as First O. But look out! There is a catch - the
elements of First O are equal to local O's Words, but the elements
of First O are Special Context Words. To obtain a list of Words
really local to O, one must do:
words-local-to-o: bind first o in o 'self
c) Function Contexts are Local Contexts too. They are attributes
of functions and are created when the respective functions are
created. Function Contexts contain: all argument Words, all
refinement Words, all optional argument Words and all local Words
specified by /local refinement. (you can verify it for particular
examples by using Same-context? function).
d) Use Contexts are Local Contexts and are created when the
respective Use function is evaluated. They contain the Local Words
specified by the first argument of Use.
e) Special Contexts - see a note under Object Contexts, other
example:
a: "q"
a: make block! a
get first a
Here First A is a Special Context Word too.
The problem is, that Bind doesn't work for Special Contexts, which
means, that our Same-context? function doesn't work too. Here is
an improved version:
special-context?: func [
{determines, if a word is a Special Context Word}
word [word!]
] [
error? try [error? get/any word]
]
; this function can help us explore the Contexts, that aren't
directly available
same-context?: func [
{find out, if the given Words are the Words of the same
Context}
word1 [word!]
word2 [word!]
] [
either global? word1 [
global? word2
] [
either special-context? word2 [
special-context? word1
] [
same? word1 bind global word1 word2
]
]
]