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

Blaise at blaise.ru Blaise at blaise.ru
Wed Dec 16 08:54:21 CET 2020

On 16.12.2020 0:07, Sven Barth wrote:
> No, those two are in fact correct this way.

Is that the /team's/ consensus?

> The modeswitch Result enables the use of Result as an alias for *any* routine …

Incorrect. The identifier Result does not alias the routine, it aliases the routine's return value.

> … that returns a result no matter if an explicit result name is set or not.

I maintain that such approach is an orthogonality violation. In light that FPC provides a syntax for explicitly naming the return value, the consistent and conceptually simplest way to treat Result as the implicit/default name for that value, not as an alias to a name that cannot even be specified in most cases (non-operators).

Were the presented cases examined when the current behaviour was implemented? And, if so, what were the points defending it? Does anyone actually think that there is even one good theoretical or practical reason to reject the following?
{$modeswitch result+}

// ACTUAL: 'Error: Duplicate identifier "result"'
operator / (const L, R: Char) result: Char;
	result := 'Z'
The compiler basically says: "You cannot name the return value Result because /I/ am naming the return value Result!". Quite ridiculous and not in the spirit of Pascal.

The following behaviour also fits my narrative rather nicely:
{$modeswitch result-}

// ACTUAL: 'Fatal: Syntax error, "identifier" expected but ":" found'
operator * (const L, R: Char): Char;
Here, rightfully, the compiler does not threat these features as separate/orthogonal: "You opted out of the default name for the return value, do provide an explicit name."

> Additionally the following will fail as well (this is allowed in mode Delphi however):
> function Result: LongInt;

That is not quite relevant for this discussion. A function and its return value are different entities.


More information about the fpc-devel mailing list