Using non-logical variables
\dlpindex{non-logical variable}
The next language extension concerns
the use of non-logical variables local to an object
to store persistent values.
Again, the counter may serve as an example.
\Ex 4: counter
.ds ctr1.pl
This example differs from the previous solution in that the
object oriented behavior is not achieved by keeping the
value of the counter as an argument in a tail-recursive loop,
but as an explicit (non-logical) variable that can be updated by
assignment.
The declaration
\dlpindex{\varexpr}
var num=0.
creates such a variable, and initializes it to zero.
The variable can be accessed by the expression .
The convention is used that when occurs on the
left hand side of the equality symbol that the value on the right hand
side is stored as the value of the variable num.
Communication with a counter of this kind, incrementing and asking for
its value, does not take place by means of input and output
statements over channels, but by using the parameters
of a goal, as illustrated in
:- O = new(ctr), O!inc(N), O!value(N1).
The call , with O referring to an object, represents a
synchronized version of remote goal evaluation.
Note that O is bound to an object and not to a channel as
in the previous example of a counter.
Synchronous remote goal evaluation may be defined by the user as
O!G :- Q = O!G, Q?.
In contrast to the synchronous communication over channels
the input part
is missing.
Conceptually, however, an answer statement answer(p1,...,pn)
containing the name of the goal in its argument list
may be regarded as such.
The goal amounts to binding the variable N to zero,
the value of the local variable num, with as a side-effect an
increment of this variable by one.
This example directly illustrates the analogy between
answering a method call and evaluating a goal. (Cf. [Am87].)
Side-effects with respect to the non-logical variables, however,
will not be undone on backtracking.