2

I have two DWScript unit files:

unit unit1;                    | unit unit2;
                               | 
interface                      | interface
                               | 
procedure Proc1exec;           | procedure Proc2exec;
procedure Proc1;               | procedure Proc2;
                               | 
implementation                 | implementation
                               | 
uses unit2;                    | uses unit1;
                               | 
procedure Proc1exec;           | procedure Proc2exec;
begin                          | begin
  unit2.Proc2;                 |   unit1.Proc1;
end;                           | end;
                               | 
procedure Proc1;               | procedure Proc2;
begin                          | begin  
end;                           | end;

During compilation of unit1 I get

ECompileError with message 'Unknown Name "unit1.Proc1"'

with the following text in IdwsProgram.Msgs.AsInfo:

Syntax Error: Unknown name "unit1.Proc1" [line: 14, column: 9, file: unit2]

Kindly explain how could I compile such circular referenced units.

Edit: In order to make the discussion closer to my requirements, I'll reformulate the question. Why DWScript does not allow me to compile these two units?

Vlad
  • 43
  • 6

2 Answers2

0

One solution is to put everything in one unit.

Another one is:

unit shared;

interface

procedure Proc1exec;
procedure Proc2exec;

implementation

uses unit1, unit2;

procedure Proc1exec;
begin
  unit1.proc1;
end;

procedure Proc2exec;
begin
  unit2.proc2;
end;

and

unit unit1;

interface

procedure Proc1;

implementation

procedure Proc1;
begin
  // do something useful
end;

end.

And something similar for unit2.

Now your code only has to include:

program Test;

uses
  unit1, unit2, shared;

begin
  Proc1exec;
  Proc2exec;
end.
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Equally one could put Proc1Exec and Proc2exec into separate units, which although not necessary in this obviously MVCS might be in the real code. In practice splitting a unit into two can be quite difficult, but is an essential part of refactoring and usually worth the effort in the long run. – Dsm Sep 13 '17 at 12:58
  • @Dsm: Even assuming that, in reality, this is a more complex scenario, I see no good reason to make too many units, especially since these two seem to be heavily dependent on each other. So I would either have one shared unit, or put everything in one unit. – Rudy Velthuis Sep 13 '17 at 13:00
  • From my perspective it depends. If Proc1Exec and Proc2Exec are closely related to each other, then fair enough, put them in one unit. If not, I would segregate. – Dsm Sep 13 '17 at 13:49
  • @Dsm: This is all rather theoretical. If we knew what these procedures do, it would be easier to make such a decision. – Rudy Velthuis Sep 13 '17 at 14:44
  • Agreed, and that was really my point. It was in no way meant as a criticism of your answer. – Dsm Sep 13 '17 at 14:47
  • @Dsm: Understood. I didn't see it as such. – Rudy Velthuis Sep 13 '17 at 14:52
  • Gents, thanks for the comments. I should have explained the requirements in more details, my fault. Splitting the code into different units is mandatory, there might be hundreds of units with their own distinct logic with ability to "jump" from one unit to another via *exec procedures called from the host application. May be the right question would be why DWScript does not allow me to compile two units from above? – Vlad Sep 13 '17 at 20:48
  • @Rudy Velthuis: Tried the approach with shared unit you suggested (after adding "uses shared;" to unit1), but DWScript still gives the same exception during compilation. I have a feeling DWScript is plagued with some bug in this regard. – Vlad Sep 13 '17 at 21:46
  • @Vlad: where in my code do I add `uses shared;` to unit1? That is another cyclic reference, and that doesn't solve the problem. You must *remove* any cycles, like I do in my answer. – Rudy Velthuis Sep 14 '17 at 08:13
  • @Rudy Velthuis: you are right. I was still compiling "unit1". Instead, "shared" unit should be feeded to dwscript as a starting point. This way it works indeed, thank you, although I'm still puzzled why my original units wouldn't compile. – Vlad Sep 14 '17 at 11:26
0

At the end I switched to PaxCompiler. It handles the units from original post without issues. It is also easy to use and covers my requirements completely.

Vlad
  • 43
  • 6