[fpc-devel] Explicitly named return values and implicit aliases Result

Tomas Hajny XHajT03 at hajny.biz
Wed Dec 16 13:20:38 CET 2020


On 2020-12-16 12:25, Blaise--- via fpc-devel wrote:
> On 16.12.2020 12:24, Michael Van Canneyt via fpc-devel wrote:
>> To be correct: Result is not the name of the result value, it is an 
>> alias.
> 
> I did not dispute that. The important point here is: "an alias" to 
> /what/?
> 
>> You can still use the function name for the result, so "Result" is in 
>> fact an alias for the function name
> 
> Result is not "an alias for the function name" because you cannot use
> Result to refer to the function:
> -------8<-------
> function Foo: Integer;
> var X: function : Integer;
> begin
> 	Result(); // ERROR
> 	X := Result; // ERROR
> end;
> -------8<-------

It is a _local_ alias for the name of the _currently_executed_ function 
which is valid just within the scope of that function. When invoking the 
function recursively, you refer to _another_ instance of that function 
and thus the alias is not valid at that point in time, because it 
becomes an alias again only when the recurrently called function starts.

Thus it's equivalent to the following example (it compiles due to {$MODE 
TP}; in other modes the local can be replaced with another name with the 
same effects):

{$MODE TP}
  function Foo: Integer;
  var
   X: Integer absolute Foo;
  begin
   X := 123;
  end;
begin
  WriteLn (Foo);
end.


>> the function name, which is the actual name for the result value.
> 
> I can get behind this notion; however, within the function, that name
> is overloaded. Thus, we should not say, like Sven did, that Result
> aliases the function (name); we should say, like I did, that it
> aliases the return value (name).
> 
> But, conceptually, I would rather look at the function name resolving
> to the return value as a backward-compatible alias, and say that
> Result is the true (default) name for the return value. Such notion
> fits modern usage.

Modern or not - use of the function name for assignment of the return 
value is part of Pascal definition, isn't it? What's the point of 
changing the "true name"?

Tomas


More information about the fpc-devel mailing list