[Oberon] Block objects for Oberon
Richard Hable
informujo at aon.at
Wed Apr 8 20:18:49 CEST 2020
On 07.04.20 23:07, Joerg wrote:
> Why not turn the logic around and use standard Oberon like this?
>
> PROCEDURE Client;
> VAR
> sum: INTEGER;
> collection: Collection;
> obj: Object;
> BEGIN
> sum := 0;
> collection.first(obj);
> WHILE collection.forEach(obj) DO
> sum := sum + obj.value
> END
> END Client;
The implementation of iterators would be a lot more complicated, because
you’d have to store the current state of the iterator within the passed
object. If the collection was implemented via some recursive pointer
structure, this could become quite a challenge.
Also, the solution is less efficient, because it requires a type-bound
procedure call with parameter passing and stack frame allocation for
each iteration. The block object, instead, can be called without
parameter passing, and re-uses the existing frame of the caller.
For comparison, here the simple implementation of the iterator procedure
with block objects: no explicit state handling, just straight-forward
traversal of a data structure.
PROCEDURE (collection: Collection) ForEach (VAR obj: Object; dolt: BLOCK);
VAR x: Object;
BEGIN
x := collection.firstObj;
WHILE x # NIL DO obj := x; dolt; x := x.next END
END ForEach;
--
Richard
More information about the Oberon
mailing list