On 4 Mar 2005 at 6:49, Frank Heckenbach wrote:
[...]
I agree with Frank that "Result" should behave as in Delphi and it does consistently behave there the way it was designed to, although the example shows that the whole idea of an "unnamed" Result identifier is in itself a poorly designed language feature. Obviously, not much thought went into it.
Indeed! I found out it's much worse than I thought ...
Consider the following program:
program Foo;
var Result: Integer;
function f: Integer; begin f := 1; Result := 2 end;
begin Result := 3; WriteLn (f, ' ', Result) end.
In any dialect that doesn't have a built-in `Result' (e.g. EP and even BP!), the assignment goes to the global `Result' variable, so the program writes `1 2'. (This is what GPC currently does, as well as BP.)
With Delphi, it assigns to the function result, however. So I guess it writes `2 3', does it?
Yes.
So, apparently, `Result' is not just another predefined identifier of a dialect like so many others. Because it's not defined at the outermost scope, but implicitly in each function,
Yes.
it can in fact alter the behaviour of the same program in an incompatible way, as this example shows.
Yes, it possibly can. However, that outcome is fully understood (and expected) by Delphi programmers. Indeed, I have never heard of it being an issue, except perhaps in a contrived situation like this.
This makes it a really evil feature ...
I disagree. Any feature can be misused by careless programming. To me, it is not much different from;
program foo; var c : integer;
procedure bar; var c: integer; begin c := 4; writeln (c); end;
begin c := 2; bar; writeln (c); end.
In either case, the person who is assigning a value to "c" ought to know which "c" he means. Same with "Result". The programmer should know which "Result" he is assigning a value to. The main difference (which some may see as vital) is that, in the case of "Result", one is explicit and one is implicit. However, I don't see that difference as fundamental, since anyone who reads the code and understands the implict "Result" variable of functions knows which "Result" is being assigned.
BTW, I wonder if Borland themselves noticed this straight-out incompatility between their languages, or what other more or less Borland compatible compilers do ...
There is no incompatibility, since BP knows nothing about "Result". If, in your example program, you really want to assign a value to the global "Result" variable, then that can be done explicitly: "Foo.Result := 2"
So what should we do?
Nothing. Programmers should know what they are doing, and it is very easy to assign to the global variable if that is what is desired. And if they don't do that, then it is clear that what is being assigned inside the function is the function's own implicit Result variable.
Best regards, The Chief -------- Prof. Abimbola A. Olowofoyeku (The African Chief) web: http://www.greatchief.plus.com/