[Lazarus] 'with' is evil, isn't it?

Martin lazarus at mfriebe.de
Thu Aug 4 13:32:16 CEST 2011


On 04/08/2011 12:10, Alexander Klenin wrote:
> 2011/8/4 Max Vlasov<max.vlasov at gmail.com>:
>> Anyone who written something in Delphi control-related with a construction
>>
>> with Canvas do .... Width Height ...
>>
>> (meaning the bounds of the control)
>> will be affected while porting something to Lazarus , because lcl Canvas has
>> width and height while Delphi Canvas not.
> Note that my proposal of using aliases would prevent this problem.

IF something like alias were to be introduced (and I am *not* for this), 
then pascal style, with a declaration above the code. It could then be 
used in a "with" statement, or anywhere else.

procedure foo;
var
   c: TObject; alias;
begin

OR
procedure foo;
alias
   c: TObject;
begin


and then
   with c = SomeFoo do c.DoBar

OR
   c = SomeFoo; // "=" not ":=" it's an alias, it isn't an assignment
   c.DoBar;

Of course, the question is why? IT had to be defined where the 
difference to a normal variable is?
- Maybe refcounting? in "with" context, thatapplies for interfaces, 
since you can not do "with stringtype do"

Some people seemed to have argued "it saves a variable" => but whta does 
that save? a few bytes on the stack or a register? that isn't even 
guaranteed.
AFAIK, in current "with" the compiler allocates room for a temp variable 
=> afaik it may even increase ref count of an interface? (But it does 
not gurantee this ref-count to be decreased at the end of the with 
block, afaik it does at the end of procedure (but that is just current 
implementation, and can't be relied on. Some people will remember, there 
was a discussion, using ref-counted interfaces in "with", for exactly 
the purpose of having them auto-freed at the end of the with block (so 
do format the output of a log) => and it does not work that way)

So currently "With" does NOT save a variable (it only saves the reader 
from spotting the variable).

On the other hand, if it truely was an alias, then if it was set to an 
expression:
    with c = SumeFuncWithSideEffects(1,2,3) do begin
     c.DoBar;
     c.DoOther;
   end;

should c expand each time, and call the function (with side effects) or 
should c alias the result. And how could one (as programmer) express, if 
one wants to allias the function-call (including specific param list), 
or the result ?

So aliases, if existent could be used in "with" but are not something 
special to (or limited to) "with". Aliases would be a generic thing 
(pretty much like "absolute", only they can be set to alias difference 
things in the code block).






More information about the Lazarus mailing list