Thread View: comp.std.c++
3 messages
3 total messages
Started by Lionel Moser
Fri, 15 Sep 1995 13:48
Re: Pascal-like 'with' statement feasible in C++?
Author: Lionel Moser
Date: Fri, 15 Sep 1995 13:48
Date: Fri, 15 Sep 1995 13:48
60 lines
2734 bytes
2734 bytes
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. ]
Re: Pascal-like 'with' statement feasible
Author: Steve Clamage
Date: Sat, 16 Sep 1995 17:42
Date: Sat, 16 Sep 1995 17:42
75 lines
3429 bytes
3429 bytes
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. ]
Re: Pascal-like 'with' statement feasible in C++?
Author: David Chase
Date: Sat, 16 Sep 1995 17:42
Date: Sat, 16 Sep 1995 17:42
49 lines
2120 bytes
2120 bytes
> 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads