Forward References

Newcomers to Astrobe and Oberon are welcome to ask any beginner-level questions here

Forward References

Postby gray » Mon Mar 04, 2019 9:14 pm

I need to reference procedures that are defined further down in the program text. It's not possible to re-arrange the code in a way that this can be avoided (use case: a state machine, where each state is represented by a procedure, and certain state changes will require forward refs). (Astrobe) Oberon does not seem have a language facility for forward refs -- I scanned the Oberon docs and books for any hints or examples, to no avail, but then again, I might have missed them.

Of course, the problem can be solved using procedure variables.

Did I miss something, or are procedure variables the only solution?
gray
 
Posts: 51
Joined: Tue Feb 12, 2019 2:59 am

Re: Forward References

Postby cfbsoftware » Tue Mar 05, 2019 10:53 am

You are right. When programming in Oberon-07, in general the best solution (when you can't otherwise avoid calling a procedure that is not defined until later) is to use a procedure variable.

However, in some scenarios a nested procedure might be a preferable alternative e.g. one of Wirth's examples is as follows:

If, for example, P calls Q and Q calls P, the forward reference might possibly be eliminated by nesting, namely if one of the procedures is called only from within the other:
Code: Select all
PROCEDURE P;
  PROCEDURE Q;
  BEGIN … P …
  END Q;
BEGIN … Q …
END P;
cfbsoftware
Site Admin
 
Posts: 395
Joined: Fri Dec 31, 2010 12:30 pm


Return to Getting Started

cron