[fpc-devel] Parameters must match exactly?

Alexander Klenin klenin at gmail.com
Thu May 20 13:57:10 CEST 2010

On Thu, May 20, 2010 at 19:31, Florian Klaempfl <florian at freepascal.org> wrote:
> Alexander Klenin schrieb:

>> I think it is a typical sample of change required to production code
>> by the restriction we are discussing.
>> Looking at the diff, I'd argue that:
>> 1) Old version was cleaner.
>> 2) New version is _not_ any safer, since any type error in the old code
>>   will result in exactly the same consequences with the new one.
>> =>
>> So, the additional inconvenience to developer did not pay off.
> It does. Everybody knows now that there is happening something strange
> because type casts are used.

I am not so sure -- the chance to catch a type error at compile-time is
just the same as before (that is, absent).
And the need to cast an object to its base class is rather confusing,
so unprepared developer will probably just write it off as a compiler quirk.
This might be alleviated by adding a comment, but:
1) Such a comment will be longer than the code it explains
2) If a compiler feature requires a comment every time it is used,
  something is not well ;-)

>> To really address the problem, I think compiler should insert a run-time type
>> check (e.g. the as operator) automatically.
> When? Before and/or after the call?

After, of course. There is nothing to check before.
To be more precise, the old code was:

InitHelper(FFont, TFont);

it is now converted to

InitHelper(TFPCanvasHelper(FFont), TFont);

the safe version would be

var tmp: TFPCanvasHelper;
InitHelper(tmp, TFont);
tmp := FFont as TFont;

I suggest that compiler should generate the code equivalent to
the third version based on the source from the first.
This would be backwards compatible, and a strict improvement
over 2.4.0 behavior.

Alexander S. Klenin

More information about the fpc-devel mailing list