[fpc-pascal] with statement using mulltiple objects

Sven Barth pascaldragon at googlemail.com
Sat Sep 13 22:03:42 CEST 2014


On 13.09.2014 21:29, vfclists . wrote:
> What then is the nesting for if the command will only apply to the last
> item with the property? Is each nested element supposed to be a property
> of the enclosing element?
>
> What is the rationale for such a statement, ie using multliple elements?

Consider this example:

=== code begin ===

type
   TTest1 = class
     procedure Foo;
   end;

   TTest2 = class
     proceduer Bar;
   end;

// implementation of TTest1 and TTest2

var
   t1: TTest1;
   t2: TTest2;
begin
   with t1, t2 do begin
     Foo; // calls t1.Foo
     Bar; // calls t2.Bar
   end;
   // same as:
   with t1 do begin
     with t2 do begin
       Foo;
       Bar;
     end;
   end;
end.

=== code end ===

"with" simply allows you to avoid some typing and evaluatios the 
expression between the "with" and "do" only once (take a function for 
example instead of "t1"). So mostly it's merely syntactic sugar.

Regards,
Sven



More information about the fpc-pascal mailing list