🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1973

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

#1973
From: Lionel Moser
Date: Fri, 15 Sep 1995 13:48
60 lines
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. ]
Technical Details
Message-ID: <fjh-950915-234944@cs.mu.oz.au>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 15 Sep 95 13:48:53 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
Message-ID: <fjh-950915-234944@cs.mu.oz.au>
From: moser@bnr.ca (Lionel Moser)
Organization: Bell-Northern Research Ltd.
Subject: Re: Pascal-like 'with' statement feasible in C++?