On Thu, Dec 05, 2002 at 05:12:49AM +0100, Frank Heckenbach wrote:
Eike Lange wrote:
On Wed, Dec 04, 2002 at 05:56:09AM -0800, Frank D. Engel, Jr. wrote:
It is exactly as it should be. In the first declaration, you redefine 'Integer' to be a variable, not a type. Then when you reach the second one (for 'Real'), Integer is no longer a type, and you get an error. In your second form, 'Integer' is still a type, since both variables are being defined simultaniously. Therefore that one works.
I ever thought, that all things right of the colon must be types. Therfore, my example is right. A redifinition of a type is IMO of the following form:
type Integer = Cardinal (7);
Where var Integer: Integer; defines a vaiable called "Integer" of type Integer, which is completly different.
Question: Why would you want to do this anyway? It is confusing.
I just wanted to test this feature. Never thought of using it in "real programs", but it should be possible.
Maybe you had the idea that an identifier could have different meanings as a type and a variable at the same time. That's not so. In Pascal, an identifier can have (at most ;-) one meaning in a scope, so when it's declared as a variable, the type declaration is "shadowed".
In your second form, 'Integer' is still a type, since both variables are being defined simultaniously. Therefore that one works.
Actually, I'm not sure. I'd have to check the standards again, but ISTR that an identifier cannot in one scope be used with an outer-scope meaning and be redefined. So if I'm not mistaken, GPC is
Indeed. Here are the relevant sections of the standard:
6.2.2.4
The scope of each defining-point shall be its region(s) (including all regions enclosed by those regions) subject to 6.2.2.5 and 6.2.2.6.
6.2.2.5
When an identifier or label has a defining-point for region A and another identifier or label having the same spelling has a defining-point for some region B enclosed by A, then region B and all regions enclosed by B shall be excluded from the scope of the defining-point for region A.
6.2.2.8
Within the scope of a defining-point of an identifier or label, each occurrence of an identifier or label having the same spelling as the identifier or label of the defining-point shall be designated an applied occurrence of the identifier or label of the defining-point, except for an occurrence that constituted the defining-point; such an occurrence shall be designated a defining occurrence. No occurrence outside that scope shall be an applied occurrence.
6.2.2.10
Required identifiers that denote the required values, types, schemata, procedures, and functions shall be used as if their defining-points have a region enclosing the program (see 6.1.3, 6.4.2.2, 6.4.3.4, 6.4.3.6, 6.4.3.3.3, 6.7.5, 6.7.6, and 6.10).
6.2.2.11
Whatever an identifier or label denotes at its defining-point shall be denoted at all applied occurrences of that identifier or label.
6.4.1 Type-definitions
[...]
The occurrence of an identifier in a type-definition of a type-definition-part of a block, a module-heading, or a module-block shall constitute its defining-point for the region that is the block, the module-heading, or the module-block. Each applied occurrence of that identifier shall be a type-identifier. [...]
6.5.1 Variable-declarations
[...]
The occurrence of an identifier in the identifier-list of a variable-declaration of the variable-declaration-part of a block, a module-heading, or a module-block shall constitute its defining-point for the region that is the block, the module-heading, or the module-block, respectively. [...]
So, following these rules,
var Integer: Integer;
is invalid: "Integer" denotes a variable in the whole block containing this declaration, and in particular, on the right-hand side of this declaration, where a type-name is expected instead.
not correct -- the second example should also fail. (But this might be harder to fix ... one might have to flag each identifier which is used in the current scope, so a later redeclaration in the same scope could be recognized as erroneous ...)
If I were writing a Pascal compiler, I would be tempted to make some of these identifiers into keywords, for this very reason. Of course, then I would not meet the Pascal standards...
Well, I wouldn't try this. We have enough problems with actual keywords from different standards/dialects (most prominently proably `value' which is a keyword in EP, but is often used as an identifier otherwise).
And if you think noone would ever want to redefine `Integer' ... well, for very strict BP compatiblity we even have to do this (define it as a 16 bit type, though only in the `System' unit with a special conditional) ...
The way it is done in the System unit is invalid as well :-)
type
Integer = Integer (16);
As above, this should mean an attempt to make a circular definition. (Of course, the System unit is full of nonstandard features anyway, so this is not a real problem.)
Emil