Forum > Suggestions

Are forward declarations really necessary?

(1/4) > >>

Ryan J:
In all of my Pascal projects I end up having a "Base" or "Global" unit which has abstract classes I need to inherit from and some virtual methods which are used to avoid circular references. Many of these can be solved by "uses" in the implementation section and declaring types in the interface section but there's always a few cases where this doesn't work.

It's not just annoying to write but it also has downstream effects where I need to cast to the concrete class in all subsequent files and override virtual methods which has overhead. I'm sure everyone knows what I mean so I won't give any examples.

My questions is, are these are specific limitations or language philosophies that means this can't be solved? I feel like it's in a deficiency of the compiler and it could be resolved using a second pass type resolution system (many languages do this already).

cdbc:
Hi
I guess, it's the price we pay, for a 'One Pass Compiler'... ;)
Have you tried to work with interfaces, both COM & CORBA, they'll give you more freedom...  8)
Regards Benny

TRon:

--- Quote from: Ryan J on February 18, 2024, 10:03:13 am ---I feel like it's in a deficiency of the compiler and it could be resolved using a second pass type resolution system (many languages do this already).

--- End quote ---
There will not be additional passes added to the compiler. This has been discussed multiple times and the standpoint was made clear for each and every discussion. I do not believe that view has changed.

Thaddy:

--- Quote from: Ryan J on February 18, 2024, 10:03:13 am ---My questions is, are these are specific limitations or language philosophies that means this can't be solved? I feel like it's in a deficiency of the compiler and it could be resolved using a second pass type resolution system (many languages do this already).

--- End quote ---
Pascal aims to be a single pass compiler (well, FPC isn't strictly speaking). That means that the compiler needs some kind of look ahead to be able to resolve constructs (classes, methods, etc) that at some point are not fully defined yet.
Forward declarations exist just because of that.
Multi-pass compilers are dead slow. Single pass compilers much faster, but at a cost. This is one of them.
I have some code written in C++ with an equivalent in FreePascal where C++ takes 3 minutes and FPC takes 3 seconds to compile.
So yes, for a Pascal compiler a mechanism like forward declarations is necessary.
That does not mean you can not write a multi pass Pascal compiler, the old and abandoned GNU pascal comes to mind, but is a philosophy you either love or hate: loose compile speed vs very little gain in optimization.

But  you can make the compile cycle slow and multi pass by using WPO  :) :D what is great for release builds. To my mind there is not much conceptual difference between WPO and a C++ optimizing compiler.

But actually you know all that....

(WPO really works a treat on large projects, btw)

marcov:

--- Quote from: Ryan J on February 18, 2024, 10:03:13 am ---My questions is, are these are specific limitations or language philosophies that means this can't be solved? I feel like it's in a deficiency of the compiler and it could be resolved using a second pass type resolution system (many languages do this already).

--- End quote ---

As with many features there are advantages and disadvantages to both methods, and the ones you stress matter how the choice is made.

The second pass resolution system has to merge different type of information of various passes, and ambiguity can happen.  Quite often the differences are especially with regards to quality error generation in cases when there is something wrong with the structure.

Also you only get most errors if the whole file can be parsed , while now you get most errors directly. A big disadvantage IMHO. When you are rearranging stuff you must first get a global situation roughly parsing before the real errors come.

Anyway it is all academic since this is so fundamental to the compiler it most likely won't ever change. If you change it, it is a different product.

But it would be interesting how Java/.NET-Pascal hybrids like PascalABC.NET and Oxygene deal with this.

Navigation

[0] Message Index

[#] Next page

Go to full version