Why the with-statement should not be extended
N. Ismail commented on my last post about the with-statement:
"I recommend the with statement be extended to allow it to also be written as such:
with a: Form1 do
begin
a.foo := 123;
a.bar := 'xyz';
a.Caption := 'Test';
Caption := 'Foo Bar'; //The forms caption
end;"
IMO, the with statement should not be extended.
Here are my reasons:
#1. This will help nothing for existing code. Existing code uses with the old way, so the compiler would have to support that syntax for foreseeable future.
#2. Enhancing the with-statement will encourage increased use of it, which is a bad thing, IMO. My suggested warning should encourage decreased use.
#3. What you want can easiliy be obtained today and work in all versions of Delphi. Just define the identifier as a local var and get rid of the with, like this:
var
a: TForm1;
begin
a := Form1;
a.foo := 123;
a.bar := 'xyz';
a.Caption := 'Test';
Caption := 'Foo Bar'; //The forms caption
end;
"Obviously one could have declared a variable named 'a' of same type as Form1,"Exactly ;)
"but I think the above syntax is cleaner"
I don't. And it doesn't give you anything functionally that a local variable cannot give you. Its pure syntactic sugar (with a sour taste, IMO).