Topic: Pascal-like 'with' statement feasible in C++?


Author: "Eugene Radchenko" <eugene@qsar.chem.msu.su>
Date: 1995/09/14
Raw View
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?
(IMHO, it is the only one nice Pascal feature (now that bitsets are (soon)
available) amiss from C++. For those not familiar with the topic, here is a
brief (and possibly not quite exact) explanation:
Suppose you have the following declarations:
  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;
I think the first version is clearer and probably more easily optimizable
than the second.
As far as I understand, in C++ I can achieve roughly the same goals for
static class members by including the 'using namespace tpair;' directive,
while for non-statics it is not possible (there is no way to indicate the
object which should be used).

         Best regards                      Genie

--
--------------------------------------------------------------------
Eugene V. Radchenko           Graduate Student in Computer Chemistry
E-mail: eugene@qsar.chem.msu.su                Fax: +7-(095)939-0290
Ordinary mail:  Chair of Organic Chemistry, Department of Chemistry,
                      Moscow State University, 119899 Moscow, Russia
*****************  Disappearances are deceptive  *******************


---
[ 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. ]





Author: moser@bnr.ca (Lionel Moser)
Date: 1995/09/15
Raw View
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?
|> (IMHO, it is the only one nice Pascal feature (now that bitsets are (soon)
|> available) amiss from C++. For those not familiar with the topic, here is a
|> brief (and possibly not quite exact) explanation:
|> Suppose you have the following declarations:
|>   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;
|> I think the first version is clearer and probably more easily optimizable
|> than the second.
|> As far as I understand, in C++ I can achieve roughly the same goals for
|> static class members by including the 'using namespace tpair;' directive,
|> while for non-statics it is not possible (there is no way to indicate the
|> object which should be used).
|>
|>          Best regards                      Genie
|>

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;

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.

This type of behaviour can be a disaster in large software systems.

-----------------------------------------------------------------------
Lionel Moser
Bell-Northern Research
Montreal, Canada

The opinions expressed are not necessarily those of my employer.
------------------------------------------------------------------------
---
[ 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. ]





Author: lhunter@acm.org (Larry Hunter)
Date: 1995/09/16
Raw View
In article <MATT.95Sep14102849@physics10.Berkeley.EDU>, "Eugene Radchenko" <eugene@qsar.chem.msu.su> says:
>whether it is possible/worthwhile to introduce the analog of Pascal 'with'
>statement into C++ ? has someone proposed/considered it?

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

You can use a reference in a block to get the same effect:

for (i=1;i<11;i++)
{pairs-element-type &X = pairs[i];
 X.m = X.n;
}

This uses features already in C++ and does not depend on an
implicit qualification by 'pairs[i].'.


---
[ 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. ]





Author: chase@centerline.com (David Chase)
Date: 1995/09/16
Raw View
> In article <MATT.95Sep14102849@physics10.Berkeley.EDU>,
  "Eugene Radchenko" <eugene@qsar.chem.msu.su> writes:

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

> |> Suppose you have the following declarations:
> |>   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;

In article <fjh-950915-234944@cs.mu.oz.au>, moser@bnr.ca (Lionel Moser) writes:
> ... can be a disaster in large software systems.

Agreed.  Note that in Modula-3, for about this reason, "with" was changed
to bind to a name, as in (using example above):

> |>   for i = 1 to 10 do
> |>     with p = pairs[i] do
> |>       p.m = p.n;
> |>   end end

This gives you most of the conciseness, with none of the problems.
Furthermore, you can get this effect in C++ with references, as in:

for (i = 1; i <= 10; i++) {
  tpair & p = pairs[i];
  p.m = p.n;
}

Modula-3 doesn't require a type, but C++ doesn't require the
do, with, do, end, end.  This is not one of the interesting differences
between the two languages.

speaking for myself,
David Chase
---
[ 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. ]





Author: danny@computek.net (Danny Bain)
Date: 1995/09/20
Raw View
On 20 Sep 95 18:43:00 GMT, shankar@engr.sgi.com (Shankar Unni) wrote:

>Kevin Lentin (kevinl@fangorn.cs.monash.edu.au) wrote:
>> With is purely a piece of syntactical sugar (with some ugly type problems
>> described elsewhere in this thread). It provides a way of making your life
>> easier (or different). Not the compiler.
>
>It is an evil piece of syntactic sugar. Wait till these "with"'s start
>getting nested, and have giant, 500+-line blocks inside them. Reading the
>resultant code is a *nightmare*.

Actually, the C++ draft includes a keyword 'using' that does what the
Pascal 'with' keyword does and many more things.  Since C++ will have
the concept of 'namespace', the 'using' keyword allows one to switch a
block of code to use the names in another namespace.

The example given is as follows:

struct B
 {
 void f(char);
 void g(char);
 };

struct D : B
 {
 using B::f;
 void f(int) { f('c'); } // calls B::f(char)
 void g(int) { g('c'); } // recursively calls d::g(int)
 }

This applies to member functions, variables, typedefs and any other
nested names declared or defined.



---
[ 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. ]