[Oberon] Procedure variables and local procedures

Andreas Pirklbauer andreas_pirklbauer at yahoo.com
Thu Sep 28 20:26:53 CEST 2017


Wojtek,
sure, this is possible - below is a slightly modified version. Compile M and N, then activate N.Run.

MODULE M;
  IMPORT Texts, Oberon;

  TYPE PROC* = PROCEDURE;
  VAR proc*: PROC;
    W: Texts.Writer;

  PROCEDURE P () : PROC;
    VAR p: PROCEDURE;

    PROCEDURE Q;
    BEGIN Texts.WriteString(W, "Q executing"); Texts.Append(Oberon.Log, W.buf)
    END Q;

  BEGIN p := Q;
    RETURN p
  END P;

BEGIN Texts.OpenWriter(W); proc := P()
END M.

MODULE N;
  IMPORT M;

  PROCEDURE Run*;
  BEGIN M.proc()
  END Run;

END N.

Local procedures are in fact almost identical to global procedures - they are compiled just like any (global) procedure, they have a fixed static starting address within the module block (where they are stored, just like global procedures). Therefore, they can of course also be assigned to variables and/or returned as the result of a function procedure, as shown in the above example.

The only difference to global procedures is that it can only be *accessed by name* (i.e. in the source code) from the enclosing scope. For example, the local procedure Q can only *accessed by name* from the enclosing procedure P - i.e. only P can reference Q by name: it might of course call Q directly, but it can also assign Q to a local or global variable. Such an assignment is perfectly legitimate (recall that internally simply the address of Q is copied).

So the fact that a local procedure Q can only be *accessed by name* from the enclosing scope does NOT mean that it somehow “disappears” once the enclosing procedure P has been executed. In other words, local procedures do NOT behave like local variables, which are allocated on the stack and indeed disappear once the procedure declaring them has been executed. By contrast, “local" procedures don’t go away, once the enclosing procedure has been executed. They are still there, it’s just that they can be accessed by their name only when the enclosing procedure executes.

Andreas

---------------------------
Thu Sep 28 19:06:44 CEST 2017 Skulski, Wojciech skulski at pas.rochester.edu <http://pas.rochester.edu/> wrote:
Andreas:

  is the following code correct? If so, can another module import M and call M.proc()? What is the intent, if it is possible?

MODULE M;
TYPE PROC* = PROCEDURE;
VAR proc*: PROC;

PROCEDURE P () : PROC;
VAR p: PROCEDURE;

      PROCEDURE Q; BEGIN END Q;

BEGIN p := Q
   RETURN p
END P;
END M.


________________________________________
From: Oberon [oberon-bounces at lists.inf.ethz.ch <https://lists.inf.ethz.ch/mailman/listinfo/oberon>] on behalf of Andreas Pirklbauer [andreas_pirklbauer at yahoo.com <https://lists.inf.ethz.ch/mailman/listinfo/oberon>]
Sent: Thursday, September 28, 2017 12:57 PM
To: ETH Oberon and related systems
Subject: [Oberon]  Procedure variables and local procedures

Addendum: Another way to read the sentence “P must not be declared local to ANOTHER procedure” is that in fact “P may be declared local to the SAME procedure”. This is the case in the example below. Either way, the assignment to procedure variables is not restricted to globally defined procedures.

AP


On 28 Sep 2017, at 18:11, Andreas Pirklbauer <andreas_pirklbauer at yahoo.com <https://lists.inf.ethz.ch/mailman/listinfo/oberon><mailto:andreas_pirklbauer at yahoo.com <https://lists.inf.ethz.ch/mailman/listinfo/oberon>>> wrote:


Note that even though the Oberon language report of Oberon-07 still states that “If a procedure P is assigned to a procedure variable of type T [...]. P must not be declared local to another procedure”, in the current implementation of Oberon-07, this restriction is not actually checked - at least not in the case when the procedure variable to which a local procedure is assigned is itself declared locally in the same scope, as in the following example:

    MODULE M;

    PROCEDURE P;
      VAR p: PROCEDURE;

      PROCEDURE Q; BEGIN END Q;

    BEGIN p := Q
    END P;
  END M.


which compiles correctly under Oberon 2013 without an error message.

AP

--------------------------------------------------------------------------

August Karlstrom fusionfile at gmail.com <https://lists.inf.ethz.ch/mailman/listinfo/oberon><mailto:fusionfile at gmail.com <https://lists.inf.ethz.ch/mailman/listinfo/oberon>> Thu Sep 18 12:14:29 CEST 2017

In the latest revision of the Oberon language, access to identifiers in
intermediate scopes is denied. Doesn't this imply that the restriction
that only globally defined procedures can be assigned to procedure
variables, is the unnecessary?

-- August

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.inf.ethz.ch/pipermail/oberon/attachments/20170928/a2e7609d/attachment-0001.html>


More information about the Oberon mailing list