It seems to me that there is a delphi extension where the variable Result can be used for any function return value. Eg:
function sum( a, b: integer ): integer; begin Result := a; Result := Result + b; end;
Given this, I expected operator to work the same way:
operator + ( a, b: integer ): integer; begin Result := a + b; end;
But this results in "parse error before `:'".
I would think the above operator code should work given the Result extension. Would it make sense to allow this? Perhaps giving an error if the Result extension is disabled?
Thanks, Peter.
Peter N Lewis wrote:
It seems to me that there is a delphi extension where the variable Result can be used for any function return value. Eg:
function sum( a, b: integer ): integer; begin Result := a; Result := Result + b; end;
Given this, I expected operator to work the same way:
operator + ( a, b: integer ): integer; begin Result := a + b; end;
But this results in "parse error before `:'".
I would think the above operator code should work given the Result extension. Would it make sense to allow this?
Just to avoid confusion, the error is not in the `Result' assignment (otherwise it would say before `:='), but in the operator heading.
That's simply because the syntax of operator definitions, according to PXSC, the only standard I know that provides them, requires an explicit result variable. In PXSC, that's a logical requirement since it's the only way to assign a result.
It's true that with Delphi (`Result') and GPC (`Return') extensions it's possible to assign a result without an explicit result variable. But still I'm not sure if it's a good thing to further bend the syntax for this reason. (FWIW, I personally don't like the implicit `Result' variable at all and never use it myself -- when I need one, I declare it explicitly. So I'm rather reluctant to change the syntax here ...)
Perhaps giving an error if the Result extension is disabled?
Strictly speaking, it's not disabled. There's just no way (syntactically) to get to it ...
Frank