Topic: Pascal-like 'with' statement feasible


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/09/16
Raw View
In article 234944@cs.mu.oz.au, moser@bnr.ca (Lionel Moser) writes:
>In article <MATT.95Sep14102849@physics10.Berkeley.EDU>, "Eugene Radchenko" <eugene@qsar.chem.msu.su> writes:
>|>
>|> Hello people!
>|> I would like to hear the opinions on the following problem:
>|> whether it is possible/worthwhile to introduce the analog of Pascal 'with'
>|> statement into C++ ? has someone proposed/considered it?
>|>...
>|>   type tpair = object
>|>          m,n: integer;
>|>          end;
>|>   var  pairs : array[1..10] of tpair;
>|> then you can write, for instance:
>|>   for i = 1 to 10 do
>|>     with pairs[i] do
>|>       m = n;
>|> instead of
>|>   for i = 1 to 10
>|>     pairs[i].m = pairs[i].n;
>|>
>
>The problem with WITH is that if the type declaration is changed to:
>
>     type tpair = object
>            n: integer;
>            end;
>
>and you have an identifier m in your scope, and it is type compatible,
>then the assignment statement in the loop body will still compile:
>
>          m = n;

Yes.

>Conversely, if the type declaration is not changed but a new variable
>m is declared and visible in the scope of the loop body, it will again
>still compile but the target of the assignment will not be what it was
>previously.

In Pascal you can declare a new variable only at the top of the function or
procedure, so that problem does not arise. But in C++ the problem could occur.

My Pascal experience has been that the "with" statement is a great
convenience in writing code, but in a large program can make the code hard
to understand. For example:
 with Object1, Object2, Object3 do
     begin
     a := b;
     c := d;
     e := f;
     end;
You cannot tell without considerable hunting where the a, b, c, d, e, and f
are located. For each identifier you look first in Object3, then in Object2,
then in Object1, then in the enclosing block scope, and so on until you
find a declaration. When you write the code you know exactly what you
are doing and it is crystal clear. A year later you come back to this
fragment and wonder what it is doing exactly. The problem isn't
necessarily conflicting identifier names as in the previous examples, but
that you can't tell from the syntax which objects are being read, and
which objects are being assigned to. (Are a,c,e from Object1 and the
rest from Object2 and Object3? Are a,c,e each from a different object?)

You can get the exact semantics of the "with" statement by declaring an
auxiliary pointer or reference for each object of the "with". The notation
isn't quite as compact:
 for( int i = 1; i <= 10; ++i ) {
  tpair& p = pairs[i]; // equivalent of "with"
  p.m = p.n;  // explict "p." needed
 }
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]