|
Yoix supports a
this
keyword without officially providing classes,
so the semantics do not duplicate what you may be familiar with from Java.
In Yoix
this
is normally used in the body of a function,
and in that case it refers to a compound object, like a
Dictionary,
that contains the definition of the function that is being executed,
which we often call the "function's context".
When a function is defined in a block
this
will be a
Dictionary
that references the local variables defined in that block.
There are no restrictions on where
this
can be used, but if it is not in the body of a function then
this
and
global
will be synonyms.
The fact that a function can be used in an assignment statement
or as the argument in another function call means the function's
context can change, and those changes are always captured by
this.
In other words, the evaluation of
this
is a runtime event and the answer is dynamic and changes when
a program "moves" a function around using assignment statements.
| |
| Example: |
The first program,
import yoix.stdio.*;
int a = 1;
{
int a = 10;
{
int a = 100;
int b = -1;
f() {
int a = 1000;
printf(" a=%d\n", a);
printf(" this.a=%d\n", this.a);
printf("global.a=%d\n\n", global.a);
if (this.a++ == 102)
printf("this=%O\n\n", this);
}
f();
f();
f();
}
}
is a simple example that prints
a=1000
this.a=100
global.a=1
a=1000
this.a=101
global.a=1
a=1000
this.a=102
global.a=1
this=Dictionary[3:0]
a=103
b=-1
>f=f()
on standard output, which illustrates how
this
works when a function is defined in a block.
The next program
import yoix.stdio.*;
String UNDEF = "<undef>";
String tree = "cedar";
Dictionary tester[0, ...] = { // growable dictionary
String tree = "oak";
SetVariables(...) {
String tree = "spruce";
String type = "blue";
printf("inside before tree: %s\n", this.tree);
printf("inside before type: %s\n",
defined("type", this) ? this.type : UNDEF);
while(argv@sizeof > 2)
this[ *++argv ] = *++argv;
printf("inside after tree: %s\n", this.tree);
printf("inside after type: %s\n",
defined("type", this) ? this.type : UNDEF);
}
};
printf("global before tree: %s\n", this.tree);
printf("global before type: %s\n",
defined("type", this) ? this.type : UNDEF);
printf("tester before tree: %s\n", tester.tree);
printf("tester before type: %s\n",
defined("type", tester) ? tester.type : UNDEF);
tester.SetVariables("tree", "maple", "type", "sugar");
printf("tester after tree: %s\n", tester.tree);
printf("tester after type: %s\n",
defined("type", tester) ? tester.type : UNDEF);
printf("global after tree: %s\n", this.tree);
printf("global after type: %s\n",
defined("type", this) ? this.type : UNDEF);
is a little more complicated and prints
global before tree: cedar
global before type: <undef>
tester before tree: oak
tester before type: <undef>
inside before tree: oak
inside before type: <undef>
inside after tree: maple
inside after type: sugar
tester after tree: maple
tester after type: sugar
global after tree: cedar
global after type: <undef>
on standard output.
Notice that the while-loop in
SetVariables
can create new entries in the
tester
dictionary, which is why we made it growable.
| | |
| See Also: |
functions,
global,
reference
|
|