The difference between one and many type blocks in Delphi

I have been programming Delphi for five or six years now, and I consider myself good enough, but I came across behavior that I could not explain. I wrote a simple linked list, I will call it TIntegerList. The sample code below compiles correctly:

type PIntegerValue = ^TIntegerValue; TIntegerValue = record Value: Integer; Next: PIntegerValue; Prev: PIntegerValue; end; 

However, the code below does not say (saying that TIntegerValue is not declared):

 type PIntegerValue = ^TIntegerValue; type TIntegerValue = record Value: Integer; Next: PIntegerValue; Prev: PIntegerValue; end; 

How exactly is the "type" keyword handled in Delphi? What is the syntactic meaning of having several types declared under the same keyword "type", compared with one "type" for each type? Well, that was weird, but I hope the sample code helps explain what I mean. I work in Delphi 2007.

+6
source share
3 answers

Logically, there is no need to use the type keyword when the code is already part of an existing type declaration section. In this way,

 type TRec1 = record end; TRec2 = record end; 

creates types that are indistinguishable from

 type TRec1 = record end; type TRec2 = record end; 

However, as you find out, the compiler has a limitation that requires all conversion declarations to be fully resolved by the end of the section where the direct declaration was introduced.

There is no particular reason for this to be so. This would be great if the compiler could mitigate this limitation. One can only assume that the detail of the compiler implementation, which probably arose a very long time ago, leaked into the language specification.

+9
source

This is the pure standard of Pascal. Since Pascal compilers are usually single-pass, and there is no direct declaration for types, this function was defined in the original Pascal by N. Wirth to allow such "recursive" types, for example. linked lists etc.

+2
source

I already gave a comment, but it should be checked by experts. It is quite effective. So here is another answer. It can be combined or whatever.

In "Algorithms + Data Structures = Programs" Wirth gives an example in "Box 4.1 direct lines"

 type ref = ^word; word = record key: integer; count: integer; next: ref end; 
0
source

All Articles