with statement using mulltiple objects

classic Classic list List threaded Threaded
60 messages Options
123
Reply | Threaded
Open this post in threaded view
|

with statement using mulltiple objects

vfclists .
According to the docs the with statement when used with mulltiple objects only works with the last parameter.


The statement

With A,B,C,D do Statement;

is equivalent to

With A do  
 With B do  
  With C do  
   With D do Statement;

-- 

I thought that if all the objects have the property the statement would apply to them eg.


with Label1, Label2, Label3 do
  Caption = 'Text'.

 
I thought all the 3 labels would have the caption set, but only Label3's caption is set. Is this a difference between Delphi's Object Pascal and FreePascal, or has Delphi's Object Pascal always worked in the same manner?


Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Luca Olivetti-2
El 13/09/14 20:13, vfclists . ha escrit:

> I thought all the 3 labels would have the caption set, but only Label3's
> caption is set. Is this a difference between Delphi's Object Pascal and
> FreePascal, or has Delphi's Object Pascal always worked in the same manner?

The "with" syntax comes from plain old pascal and it has always worked
this way.

http://books.google.co.uk/books?id=xXSZbSLFTM8C&lpg=PP1&hl=ca&pg=PA73#v=onepage&q&f=false

Bye
--
Luca

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Sven Barth-2
In reply to this post by vfclists .
On 13.09.2014 20:13, vfclists . wrote:

> According to the docs the with statement when used with mulltiple
> objects only works with the last parameter.
>
>
> The statement
>
> With A,B,C,D do Statement;
>
> is equivalent to
>
> With A do
>   With B do
>    With C do
>     With D do Statement;
>
> --
>
> I thought that if all the objects have the property the statement would
> apply to them eg.
>
>
> with Label1, Label2, Label3 do
>    Caption = 'Text'.
>
> I thought all the 3 labels would have the caption set, but only Label3's
> caption is set. Is this a difference between Delphi's Object Pascal and
> FreePascal, or has Delphi's Object Pascal always worked in the same manner?

It has always worked in this manner. It's just for abbreviating typing,
not for combining properties. (and then the with-variant with multiple
elements is even more seldomly used than the single-element one...)

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

vfclists .


On 13 September 2014 20:02, Sven Barth <[hidden email]> wrote:
On 13.09.2014 20:13, vfclists . wrote:
According to the docs the with statement when used with mulltiple
objects only works with the last parameter.


The statement

With A,B,C,D do Statement;

is equivalent to

With A do
  With B do
   With C do
    With D do Statement;

--

I thought that if all the objects have the property the statement would
apply to them eg.


with Label1, Label2, Label3 do
   Caption = 'Text'.

I thought all the 3 labels would have the caption set, but only Label3's
caption is set. Is this a difference between Delphi's Object Pascal and
FreePascal, or has Delphi's Object Pascal always worked in the same manner?

It has always worked in this manner. It's just for abbreviating typing, not for combining properties. (and then the with-variant with multiple elements is even more seldomly used than the single-element one...)

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

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?

--
Frank Church

=======================
http://devblog.brahmancreations.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Sven Barth-2
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Mattias Gaertner
In reply to this post by vfclists .
On Sat, 13 Sep 2014 20:29:32 +0100
"vfclists ." <[hidden email]> wrote:

>[...]
> What then is the nesting for if the command will only apply to the last
> item with the property?

The compiler searches the identifier in the last With, then in the
second last With, ..., then in the local variables, then in the
parameters and so forth.

> 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?

Less typing.

Mattias
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Marius2
Mattias Gaertner wrote:

>Less typing.

Just some generic comments in general.

Its also much harder to understand in the long run and it can even be
dangerous as stuff migh be refactored over time. All seasoned
programmers know that and try to avoid the with statement.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Graeme Geldenhuys-6
In reply to this post by Mattias Gaertner
On 2014-09-13 21:04, Mattias Gaertner wrote:
>> What is the rationale for such a statement, ie using multliple elements?
> Less typing.

...and increases grey hair growth (due to lots more trouble debugging). :-)


Regards,
  - Graeme -

--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Graeme Geldenhuys-6
In reply to this post by Marius2
On 2014-09-13 21:31, Marius wrote:
> programmers know that and try to avoid the with statement.

+1


Regards,
  - Graeme -

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Jürgen Hestermann
In reply to this post by Marius2
Am 2014-09-13 22:31, schrieb Marius:
 > Mattias Gaertner wrote:
 > All seasoned programmers know that and try to avoid the with statement.


Realy? I love it. Consider these two varianst of code from one of my programs:

---------------------------------------------------------
with TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^ do
    begin
    DirLogged             := true;
    DirHatFocus           := false;
    SubDirsExpanded       := true;
    NonLoggedDirsInBranch := 0;
    LastSubDirIndex       := -1;
    ErrorString           := '';
    fillchar(StatistikOfFiles,sizeof(StatistikOfFiles),0);
    fillchar(StatistikOfDirs ,sizeof(StatistikOfDirs) ,0);
    Verzeichnis           := Form1.Verzeichnisbaum.RootNode;
    DirName               := DirectorySeparator;
    DirNameLowerCase      := LowerCase(DirName);
    TRightMostPeriod      := 0;
    DirSize               := 0;
    TFileAttributes       := 0;
    fillchar(TDateWritten,sizeof(TDateWritten),0);
    TFlagSet              := [];
    SubDirs               := nil;
    Files                 := nil;
    end;
---------------------------------------------------------

and

---------------------------------------------------------
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirLogged             := true;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirHatFocus           := false;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.SubDirsExpanded       := true;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.NonLoggedDirsInBranch := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.LastSubDirIndex       := -1;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.ErrorString           := '';
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfFiles,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfFiles),0);
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfDirs ,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfDirs) ,0);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.Verzeichnis           := Form1.Verzeichnisbaum.RootNode;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirName               := DirectorySeparator;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirNameLowerCase      := LowerCase(DirName);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TRightMostPeriod      := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirSize               := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TFileAttributes       := 0;
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TDateWritten,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TDateWritten),0);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TFlagSet              := [];
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.SubDirs               := nil;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.Files                 := nil;
---------------------------------------------------------

Which one would you prefer?
The first one is *much* less error prone because it is easier to read and makes it therefore clearer than the second example.
Also, the calculation of the pointer is done multiple times in the second example which makes the code slower.

I love Pascal also because of the WITH statement.
The risks are the same as when using multiple units which define identical named identifiers.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Mattias Gaertner
On Sun, 14 Sep 2014 11:58:22 +0200
Jürgen Hestermann <[hidden email]> wrote:

> Am 2014-09-13 22:31, schrieb Marius:
>  > Mattias Gaertner wrote:
>  > All seasoned programmers know that and try to avoid the with statement.

The quote is from Marius, not from me.


> Realy? I love it. Consider these two varianst of code from one of my programs:
>
> ---------------------------------------------------------
> with TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^ do
>     begin
>     DirLogged             := true;
>     DirHatFocus           := false;
>     SubDirsExpanded       := true;
>[...]
> ---------------------------------------------------------

and

---------------------------------------------------------
d:=TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]; // local var d
d^.DirLogged             := true;
d^.DirHatFocus           := false;
d^.SubDirsExpanded       := true;
[...] ---------------------------------------------------------


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Jürgen Hestermann
Am 2014-09-14 12:08, schrieb Mattias Gaertner:
 >>  > All seasoned programmers know that and try to avoid the with statement.
 > The quote is from Marius, not from me.

Correct. I deleted the wrong reference. ;-(


 >> Realy? I love it. Consider these two varianst of code from one of my programs:
 >>
 >> ---------------------------------------------------------
 >> with TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^ do
 >>     begin
 >>     DirLogged             := true;
 >>     DirHatFocus           := false;
 >>     SubDirsExpanded       := true;
 >> [...]
 >> ---------------------------------------------------------
 >
 > and
 >
 > ---------------------------------------------------------
 > d:=TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]; // local var d
 > d^.DirLogged             := true;
 > d^.DirHatFocus           := false;
 > d^.SubDirsExpanded       := true;
 > [...] ---------------------------------------------------------


Long-winded and still not the same clarity as in the with statement.

Do you also prepent *all* function and variable names
with the unit name they come from?
Do you realy *always* write

LazUTF8.UTF8Delete
instead of
UTF8Delete
?

Nested WITH statements and the unit concept share the
same problem but still the benefits outweigh the drawbacks IMO
(once you get used to it and keep possible problems in mind).


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Sven Barth-2
In reply to this post by Graeme Geldenhuys-6

Am 14.09.2014 09:36 schrieb "Graeme Geldenhuys" <[hidden email]>:
>
> On 2014-09-13 21:04, Mattias Gaertner wrote:
> >> What is the rationale for such a statement, ie using multliple elements?
> > Less typing.
>
> ...and increases grey hair growth (due to lots more trouble debugging). :-)
>

Though to be honest Lazarus handles with-statements in context of debugging much more gracefully than Delphi...

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Luca Olivetti-2
In reply to this post by Jürgen Hestermann
El 14/09/14 12:22, Jürgen Hestermann ha escrit:

>> ---------------------------------------------------------
>> d:=TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]; // local var d
>> d^.DirLogged             := true;
>> d^.DirHatFocus           := false;
>> d^.SubDirsExpanded       := true;
>> [...] ---------------------------------------------------------
>
>
> Long-winded and still not the same clarity as in the with statement.

As I say every time this discussion comes up ;-), TI pascal had the best
of both worlds: you could either use the naked with or a named with,
which removed ambiguity, i.e.

with a=Label1,b=Label2 do
  a.caption:=b.caption;

(OK, it's a contrived example but you get the drift).

Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Marius2
In reply to this post by Jürgen Hestermann
Jürgen Hestermann wrote:

>Realy? I love it. Consider these two varianst of code from one of my
>programs:

This is a simpler example than the one with with the 4 levels deep
(which can be quite a puzzle if there are multiple records containing
the same fieldname(s)). And yes you may love them but please read on..

>Which one would you prefer?

The second, but not for readability or typing, I would suggest a third
solution by using a variable (like Matthias also showed).

The point is you are very likely to run into refactoring problems.
Suppose your routines are in 2 different methods in a class and there
is class inheritance and one of the base class has a property
errorstring. Suppose you decide to refactor the errorstring in you
record structure to something else (and forget to adjust it properly
which is human). Your first solution will compile but it would reset
the wrong errorstring, your second solution will simply not compile
because it has been prefixed (hence my preference to pick that one).
You can argue its the fault of the programmer but why create an unsafe
situation in the first place (and that just what the with statement
does)...

We have spend weeks (if not months) off hunting down this kind of bugs.
And its this kind of bug what showed up frequently in teams of
programmers and that made us decide to ban the with statement as much
as possible. Sooner or later the with stament is going to bite you, I
just hope you wont have to spend that much time on resolving it ;(


Marius

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Marius2
In reply to this post by Jürgen Hestermann
Jürgen Hestermann wrote:

>Do you also prepent all function and variable names
>with the unit name they come from?
>Do you realy always write
>
>LazUTF8.UTF8Delete
>instead of
>UTF8Delete
>?
>
>Nested WITH statements and the unit concept share the
>same problem but still the benefits outweigh the drawbacks IMO
>(once you get used to it and keep possible problems in mind).

Good point, as far as i know there is not a solid solution for units
unless those global procedures like utf8delete where rewritten as class
procedures.

Marius

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Marco van de Voort
In reply to this post by Jürgen Hestermann
In our previous episode, J?rgen Hestermann said:
>  > ---------------------------------------------------------
>  > d:=TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]; // local var d
>  > d^.DirLogged             := true;
>  > d^.DirHatFocus           := false;
>  > d^.SubDirsExpanded       := true;
>  > [...] ---------------------------------------------------------
>
>
> Long-winded and still not the same clarity as in the with statement.

But the important part is that it forces each property/field to be a member
of T, or get a compiler error.

WITH doesn't, and you risk that if your class or one of its ancestor has
such property, and your error goes unnoticed.
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Michael Thompson
In reply to this post by Marius2
> We have spend weeks (if not months) off hunting down this kind of bugs.
> And its this kind of bug what showed up frequently in teams of
> programmers and that made us decide to ban the with statement as much
> as possible. Sooner or later the with stament is going to bite you, I
> just hope you wont have to spend that much time on resolving it ;(

+1.  With statement was banned at my old job as well for exactly the reasons stated (by everyone :) )

Mike

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

philippe sylvain levi

-1. !!!

I like "with"!!!! ... even it may give trouble times to times!!! ... but I work alone on my programs ...

as always ... nothing perfect! .... always should consider the conditions!!

 

Em 14.09.2014 09:30, Michael Thompson escreveu:

> We have spend weeks (if not months) off hunting down this kind of bugs.
> And its this kind of bug what showed up frequently in teams of
> programmers and that made us decide to ban the with statement as much
> as possible. Sooner or later the with stament is going to bite you, I
> just hope you wont have to spend that much time on resolving it ;(
 
+1.  With statement was banned at my old job as well for exactly the reasons stated (by everyone :) )
 
Mike

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

 


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: with statement using mulltiple objects

Ched
In reply to this post by Marius2
Hello All,

My two cents. The with statement is crystal clear for me. If newbies prefer to use different manners to
code the things, that are free to do it. Using some kind of intermediate variable  ? That's make to code
difficult to understand, need do open begin/end blocks in some situations. With make the code very clear,
probably helps the compiler a lot. Well used, it's 100% safe - I *never* encountered any problems with it
in 25+ years of intensive programmation.

So, they with statement has at least one aficionados: me. Was the with statement present if the first
versions designed by Wirth himself ?

Cheers, Ched'





Le 14. 09. 14 12:20, Marius a écrit :

> Jürgen Hestermann wrote:
>
>> Realy? I love it. Consider these two varianst of code from one of my
>> programs:
>
> This is a simpler example than the one with with the 4 levels deep
> (which can be quite a puzzle if there are multiple records containing
> the same fieldname(s)). And yes you may love them but please read on..
>
>> Which one would you prefer?
>
> The second, but not for readability or typing, I would suggest a third
> solution by using a variable (like Matthias also showed).
>
> The point is you are very likely to run into refactoring problems.
> Suppose your routines are in 2 different methods in a class and there
> is class inheritance and one of the base class has a property
> errorstring. Suppose you decide to refactor the errorstring in you
> record structure to something else (and forget to adjust it properly
> which is human). Your first solution will compile but it would reset
> the wrong errorstring, your second solution will simply not compile
> because it has been prefixed (hence my preference to pick that one).
> You can argue its the fault of the programmer but why create an unsafe
> situation in the first place (and that just what the with statement
> does)...
>
> We have spend weeks (if not months) off hunting down this kind of bugs.
> And its this kind of bug what showed up frequently in teams of
> programmers and that made us decide to ban the with statement as much
> as possible. Sooner or later the with stament is going to bite you, I
> just hope you wont have to spend that much time on resolving it ;(
>
>
> Marius
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
123