<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Il 25/10/2017 14:12, Giuliano Colla via Lazarus ha scritto:<br>
</p>
I raised some noise about procedural variables in Delphi mode, but
it turns out that I had failed to notice some discrepancies while
porting code back and forth from Delphi to Lazarus.<br>
<br>
<ol>
<li>
<p> I had failed to notice that RAD Studio X10 when importing
the Delphi 7 project had silently dropped a declaration from
my source, and this is what gave rise to a compiler error.</p>
</li>
<li>
<p>I had failed to detect that the OnCreate event, which should
have properly initialized the procedural variable in my test
program was never triggered.</p>
</li>
</ol>
<br>
When I realized it I did refine and repeat all my tests in Kylix,
Delphi 7, X10 and fpc, and the final result is the following:<br>
<br>
1) Delphi accepts the assignment of an untyped pointer to a
procedural variable even if it is a method pointer. This is true
from Delphi 3 up to Kylix, Delphi 7 and Rad Studio X10.<br>
<br>
2) The syntax for this particular assignment (again from Delphi 3 to
X10) is:<br>
<br>
@Procvar := aPointer;<br>
<br>
3) What Delphi does in that case is: <br>
copy the pointer to the procedure in the "code" field of the
procedural variable, leaving the "data" field untouched.<br>
<br>
4) fpc in Delphi mode does exactly the same.<br>
<br>
This makes it possible in Delphi, or in Delphi mode fpc, to
initialize the procedural variable to point to a method of an
object, and subsequently to make it point to any other compatible
method of the same object, just by providing an untyped pointer,
which may be handled with the many tools which deal only with
pointers, for jump tables, queues of actions etc.<br>
<br>
Of course this is not the best thing to do. An appropriate typecast
of the procedural variable to a TMethod, and then explicitly setting
the fields would make the code much more readable. That is what
common sense and good programming practice would suggest. ObjFpc
mode only provides this second option, for the same purpose, and
this is certainly better.<br>
<br>
But, as the purpose of Delphi mode is not to provide what common
sense or good programming practice would suggest, but to mimic
Delphi behaviour, I raised the question when I detected an apparent
fpc bug.<br>
<br>
It turns out that it was my fault, so, sorry for the noise.<br>
<br>
Giuliano<br>
<br>
</body>
</html>