Consider the following situation:
program Foo;
function a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
begin WriteLn ('OK') end.
The problem here is that within the outer `a', the identifier `a' can mean both its result variable and the local function `a'.
BP allows the declaration, but not the assignment `a := a' (because apparently interprets `a' to mean the local `a'), so there seems to be no way to return a meaningful value from the outer `a' which would render the function quite useless.
In EP I don't seem to find any wording that forbids it (but it's quite confusing as usual, so I might have missed it).
GPC currently doesn't allow the declaration of the inner `a'. This seems reasonable to me, but is it conforming with the standard?
Frank
On Wed, Jan 29, 2003 at 02:03:57AM +0100, Frank Heckenbach wrote:
Consider the following situation:
program Foo;
function a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
begin WriteLn ('OK') end.
The problem here is that within the outer `a', the identifier `a' can mean both its result variable and the local function `a'.
IMHO function heading does _not_ declare the function name as a real variable identifier. It is allowed _only_ on the left-hand side of an assignment statement. Anywhere else it behaves according to the usual Pascal scoping rules: in general this means a recursive call (because the name is defined in outer scope as a function identifier), but in the example above, it is shadowed by the local function declaration. (This is legal, because the scopes are different.)
EP's result value specifications (`function b = a: Integer') are different, they do declare a real variable. The following is not allowed, because identifier `a' is defined twice in the same scope:
function b = a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
See also 6.7.2 and 6.9.2.2 of ISO 10206.
Emil
BP allows the declaration, but not the assignment `a := a' (because apparently interprets `a' to mean the local `a'), so there seems to be no way to return a meaningful value from the outer `a' which would render the function quite useless.
In EP I don't seem to find any wording that forbids it (but it's quite confusing as usual, so I might have missed it).
GPC currently doesn't allow the declaration of the inner `a'. This seems reasonable to me, but is it conforming with the standard?
Frank
-- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/, 7977168E GPC To-Do list, latest features, fixed bugs: http://www.gnu-pascal.de/todo.html GPC download signing key: 51FF C1F0 1A77 C6C2 4482 4DDC 117A 9773 7F88 1707
Emil Jerabek wrote:
On Wed, Jan 29, 2003 at 02:03:57AM +0100, Frank Heckenbach wrote:
Consider the following situation:
program Foo;
function a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
begin WriteLn ('OK') end.
The problem here is that within the outer `a', the identifier `a' can mean both its result variable and the local function `a'.
IMHO function heading does _not_ declare the function name as a real variable identifier.
Yes, it's a "functionÂidentifier" in terms of the standard.
It is allowed _only_ on the left-hand side of an assignment statement.
OK (6.9.2.2).
Anywhere else it behaves according to the usual Pascal scoping rules: in general this means a recursive call (because the name is defined in outer scope as a function identifier), but in the example above, it is shadowed by the local function declaration. (This is legal, because the scopes are different.)
But still the identifier `a' would refer to both functions (as a functionÂidentifier) on the left and right sides of the same statement?
EP's result value specifications (`function b = a: Integer') are different, they do declare a real variable. The following is not allowed, because identifier `a' is defined twice in the same scope:
function b = a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
I agree here.
Frank
On Wed, Jan 29, 2003 at 10:18:19PM +0100, Frank Heckenbach wrote:
Emil Jerabek wrote:
On Wed, Jan 29, 2003 at 02:03:57AM +0100, Frank Heckenbach wrote:
Consider the following situation:
program Foo;
function a: Integer;
function a: Integer; begin a := 42 end;
begin a := a end;
begin WriteLn ('OK') end.
The problem here is that within the outer `a', the identifier `a' can mean both its result variable and the local function `a'.
IMHO function heading does _not_ declare the function name as a real variable identifier.
Yes, it's a "functionÂidentifier" in terms of the standard.
It is allowed _only_ on the left-hand side of an assignment statement.
OK (6.9.2.2).
Anywhere else it behaves according to the usual Pascal scoping rules: in general this means a recursive call (because the name is defined in outer scope as a function identifier), but in the example above, it is shadowed by the local function declaration. (This is legal, because the scopes are different.)
But still the identifier `a' would refer to both functions (as a functionÂidentifier) on the left and right sides of the same statement?
On second sight, I'm not sure at all. The right side refers to the local function. As for the left side, I assumed in my first reply that it refers to the result variable, because it can't mean anything else (a call to the local `a' is not an lvalue), but this explanation fails in other situations:
function a: Integer;
var a: Integer;
begin a := 42 end;
It seems logical that such a local declaration shadows the outer function-identifier `a' completely (hence both examples are invalid).
Emil