Hi Andrew,

I had written:
>Which part surprises you? 

to which you replied

>
>That Interface/Test returns the value of 't! In C++, this wouldn't
>work, as the function 'Test_Implementation wouldn't be able to find 't
>in 'Interface. It's disturbing to me as a C++ programmer, but viewing
>it as a novice might, it seems perfectly correct that the copied
>function "knows" where t is.


A) While REBOL was not fashioned in the likeness of C++, what is happening
here is not unusual from a C++ point of view. After all, if you have a
class in C++ that contains a private variable and a public function, and
the public function manipulates the private variable, then you can
reference and modify the value of the private variable by using the public
function of the class. That is trivial.

B) What is happening here is similar. 
1. Test_Implementation is a word that refences the value returned by 
make function! [] [t] in Implementation's context.
2. "in Implementation 'Test_Implementation"
returns the word Test_Implementation that is bound in Implementation's
context.
3. the function "get" in the expresssion 
"get in Implementation Test_Implementation"
retrieves the value of Test_Implementation in Implementation's context,
which is the result of make function! [] [t]
4. The assignment "Test:" in "Test: get in Implementation
Test_Implementation":
Test is assigned as a reference to the function identified in the previous
step. Since 't is defined in Implementation's context and get retrieved the
function defined in Implementation's context, Interface's 'Test has now
become a reference to a function (not only the specification, the function
itself) which is implemented in and therefore has access to words defined
in Implementation's context.
5. When you evaluate Interface/Test, 
a) Test is dereferenced and evaluates to the function [] [t] defined in
Implementation's context. 
b) The function [] [t] is evaluated in Implementation's context. 
c) t is dereferenced in Implementation's context.
d) t's values is returned by the function, which was referenced by the word
Test.

In summary, Interface's word 'Test does not know of 't. 'Test only knows a
function. That function is implemented in Implementation's context and
therefore has access to words defined in Implementation's context, one of
which happens to be 't.

Compare to C++:
Define Implementation as a class.
Define t as a private variable.
Define Test_Implementation as a public function that returns whatever t
happens to contain.

Define Interface as another class.
Define Test as a public pointer to a function and assign it to point at the
public function Test_Implementation in Implementation.
Then executing Test will return the value contained in t.

Hope this helps,

Elan

Reply via email to