| ||
Tutorial: Control Structures |
Now that all the interesting stuff has been dealt with, we have to
cover a few odds and ends. This section is about the imperative
aspect of gbeta, namely the built-in imperatives for unusual but
structured transfer of control. Ah, by the way, leave
and restart are not boring, even though they are
imperative constructs.
Standard control structure imperatives
Since the 70'ties, all imperative languages (including the
object-oriented subset) have had built-in support for "structured"
versions of
As a matter of fact, BETA was designed to have a minimal collection of
such constructs, since the generality of the pattern concept allows it
to support all kinds of customized control structures. As an example,
look at
The
"switch" or
"case" statements in other languages:
<Evaluation> is computed and the value
compared for equality with the <Evaluation> s after
the "//" one by one, and the block of imperatives
(".." ) after the first matching value is executed. If no
equal values are found, the else imperatives are
executed.
Finally, iterating a pre-computed number of times is supported with
the
".."
<Evaluation> times, and the second does the same, with
<Name> denoting the number of the current iteration, i.e. 1 the
first time round, 2 the second etc. <Name> is
not an object, it just denotes the value, so it cannot be
changed by assignment, and the type of it cannot be computed (using
"##" ), and it is not allowed to ask for a reference to it
(using "[]" ).
Jumping around, and unwinding the stack
The two last constructs for the transfer of control are very
expressive, even if they look rather ordinary at the first sight.
They are called The first one:
<Name> , i.e. it forces the execution of the block
to terminate earlier than it otherwise would. The other one:
<Name> , i.e. it forces the execution of the block
to start from the beginning again.
The reason why this is not entirely trivial is that we may have (due
to the coercion) objects on the stack between the one
associated with the enclosing block and the one which is currently
executing. In other words,
This resembles the exception handling mechanisms of other languages,
e.g. the Example 10
Here is an example which by the way also demonstrates how it is
possible to create a recursive procedure which will call the
specialized version of itself in specializations. This is not a
problem with gbeta, it's an obstacle which never occurs in languages
that do not support specialization of behavior in the first place,
such as anything but BETA and CLOS.. Anyway, we need a notion of
selfType or myType or whatever else that concept has
been designated in the literature, and again coercion comes
to the rescue, since
The |