🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

6 total messages Started by Eugene Radchenko Thu, 14 Sep 1995 17:28
Pascal-like 'with' statement feasible in C++?
#1969
Author: Eugene Radchenko
Date: Thu, 14 Sep 1995 17:28
42 lines
2158 bytes
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. ]
Re: Pascal-like 'with' statement feasible in C++?
#1978
Author: Larry Hunter
Date: Sat, 16 Sep 1995 08:23
27 lines
1262 bytes
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. ]
Re: Pascal-like 'with' statement feasible in C++?
#1989
Author: Kevin Lentin
Date: Mon, 18 Sep 1995 14:59
33 lines
2057 bytes
Eugene Radchenko (eugene@qsar.chem.msu.su) wrote:
>   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.

Would make 0 difference to optimisers, I suspect. The first may look
shorter to you but it's still doing what the second one says. A compiler
would be able to recognise this in both cases.

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.

-- 
[==================================================================]
[ Kevin Lentin                   |___/~\__/~\___/~~~~\__/~\__/~\_| ]
[ kevinl@cs.monash.edu.au        |___/~\/~\_____/~\______/~\/~\__| ]
[ Macintrash: 'Just say NO!'     |___/~\__/~\___/~~~~\____/~~\___| ]
[==================================================================]

[ 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++?
#1997
Author: Shankar Unni
Date: Wed, 20 Sep 1995 18:43
17 lines
1325 bytes
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*.
--
Shankar Unni				E-Mail:	shankar@sgi.com
Silicon Graphics Inc.			Phone:	+1-415-390-2072
URL:	http://reality.sgi.com/employees/shankar
---
[ 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++?
#1999
Author: Danny Bain
Date: Wed, 20 Sep 1995 23:32
41 lines
1915 bytes
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. ]
Re: Pascal-like 'with' statement feasible in C++?
#2021
Author: Tom Payne
Date: Sun, 24 Sep 1995 06:04
75 lines
3113 bytes
Danny Bain (danny@computek.net) wrote:

: 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.


If I understand correctly, the semantics of WITH are a bit different
from those of using in that it applies to records (i.e., struct and
class objects) and causes members to appear as locally declared 
variables, shadowing others that are currently visible.

   class Whatever {
   public:
     int x;
     float y;
     void f(int i);
   };

   
   class Thingeroo {
   public:
      Watever w;
      with w;
      float g() {
         f(x);                 // w.f(w.x);
         return y;             // returns w.y
      }
   }

Syntactically, this is something like one would have if Whatever were
a base class for Thingeroo and the declaration of w were omitted.  Of
course, the WITH construction gives no dynamic binding etc..  

There are a lot of tedious details to work out with such a proposal,
e.g., ambiguity resolution and access control, but the problems are
similar to USING. 

Tom Payne (cs.ucr.edu)



[ 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