Article View: comp.lang.oberon
Article #836Why doesn't Oberon have proper procedure values?
From: Wolfgang Weck
Date: Wed, 24 Jan 1996 15:44
Date: Wed, 24 Jan 1996 15:44
71 lines
2432 bytes
2432 bytes
> For example, we can define collections that support iterator methods,
> but these methods would be more useful if their arguments could be
> local procedures. To give a trivial example:
>
> PROCEDURE sum(xs: intset);
> VAR s: integer;
>
> PROCEDURE add(x: integer);
> BEGIN s := s + x END add;
>
> BEGIN
> s := 0;
> xs.do(add);
> RETURN s
> END sum;
After we have read several replies explaining why the language does not permit
local procedures as parameters I would like to state:
You do not need it! You should use OOP instead!
Admittedly, the requirement is that the interface of your iterating procedure
is defined properly. It should use an action object (i.e. extensible record)
instead of just a procedure:
TYPE
Action = RECORD
do: PROCEDURE (VAR self: Action; x: INTEGER)
END;
PROCEDURE Do (s: IntSet; VAR a: Action);
VAR x: INTEGER;
BEGIN
.... a.do(a, x) ....
END Do;
When using it, the global state you require during iteration (s: integer in
your example) becomes an instance variable of such an object:
TYPE
Addition = RECORD (Action)
s: INTEGER
END;
PROCEDURE Add (VAR a: Action; x: INTEGER);
BEGIN
a(Addition).s := a(Addition).s + x
END Add;
PROCEDURE Sum (xs: IntSet): INTEGER;
VAR a: Addition;
BEGIN
a.s := 0; a.do := Add; Do(xs, a); RETURN a.s
END Sum;
A final note: this idea can be implemented in various ways. You can use a
pointer type instead of the action-VAR-parameter. You can use a type bound
procedure for the action's do method instead of the procedure variable. All of
this does not really matter here. I just had to choose one implementation for
the example.
ww
Wolfgang Weck
Institute of Scientific Computing
ETH-Zentrum
CH-8092 Zuerich
e-mail: Weck@inf.ethz.ch
http://www.inf.ethz.ch/personal/weck
Technical Details
Message-ID:
<199601241541.QAA25429@mendel.inf.ethz.ch>Path:
arc1-ca.pugleaf.net!inf.ethz.ch!oberon-news@inf.ethz.chShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!inf.ethz.ch!oberon-news@inf.ethz.ch From: Wolfgang Weck <weck@inf.ethz.ch> Newsgroups: comp.lang.oberon Subject: Why doesn't Oberon have proper procedure values? Date: 24 Jan 1996 16:44:40 +0100 Organization: Dept. Informatik, Swiss Federal Institute of Technology Sender: server@inf.ethz.ch Distribution: world Message-ID: <199601241541.QAA25429@mendel.inf.ethz.ch> Reply-To: oberon-news@inf.ethz.ch NNTP-Posting-Host: neptune.inf.ethz.ch Originator: oberon-news@inf.ethz.ch