Discussion:
c++ "with" keyword
Norman Jonas
2002-12-29 02:44:21 UTC
Permalink
Since the several years I develop in c++ there is nothing I missed more than
the "with"
keyword as it is known by languages like Pascal. So I would like to know
what gcc
developer / user think of the idea to advance the c++ standard to add it.

Pro:
Very useful for many c++ developer
Slightly reduces code size / code to write

Contra:
Needs time until compiler developing companies implement it

To give an example of what I think the with keyword should be used :

struct S
{
int x;
int y;
};

int main()
{
S s;
with (s)
{
.x = 1;
.y = 2;
}
return 0;
}

and / or

class C
{
public:
void init ();
void next ();
}

int main()
{
C c;
with (c)
{
.init();
.next();
}
return 0;
}

This technique could be used in conjunction with structs as well as classes.
If there is enough reason to add this keyword to the c++ standard maybe the
ANSI consortium could be asked to add the "with" keyword to a new revision
of their c++ draft.

Norman Jonas
Alexandre Oliva
2002-12-29 10:18:26 UTC
Permalink
Post by Norman Jonas
Since the several years I develop in c++ there is nothing I missed
more than the "with" keyword as it is known by languages like
Pascal. So I would like to know what gcc developer / user think of
the idea to advance the c++ standard to add it.
There's a general trend against extensions in GCC these days. I don't
always agree with this stance, but I happen to agree with it in this
case. Pascal's `with' can be pretty much modeled in OO as the
implicit this. Wishing for the presence of `with' is, IMHO, generally
an indication that the code should be in a method of the class whose
members you're accessing. Besides, people often complain that C++ has
too much implicit behavior and room for ambiguities; `with' would just
add to it.
--
Alexandre Oliva Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist Professional serial bug killer
Erik Schnetter
2002-12-29 12:27:32 UTC
Permalink
The example

struct S
{
int x;
int y;
};

int main()
{
S s;
with (s)
{
.x = 1;
.y = 2;
}
return 0;
}

can easily be rewritten by introducing temporary references, as in

int main()
{
S s;
{
S& t = s;
t.x = 1;
t.y = 2;
}
return 0;
}

This requires only one additional variable reference each time the
"with" object is used. Additionally, it allows several "with" objects
(with different names) at the same time. In C, the same thing can be
done by using pointers.

-erik
--
Erik Schnetter <***@uni-tuebingen.de>
Web: http://www.tat.physik.uni-tuebingen.de/~schnette/

My email is as private as my paper mail. I therefore support encrypting
and signing email messages. Get my PGP key from www.keyserver.net.
Norman Jonas
2002-12-29 14:06:23 UTC
Permalink
I think you missed the point. The reason for the with keyword is not to use
a pointer but to leave
the long structs name which is not done by your example :

struct S
{
char* name;
char* street;
char* city;
} verylongdescriptivename;

If you want to access several values of this struct you always have to type
in the whole name :

verylongdescriptivename.name = "hans";
verylongdescriptivename.street = "xxx 13";
verylongdescriptivename.city = "cologne";

using the "with" keyword this code becomes much smaller and cleaner :

with ( verylongdescriptivename )
{
.name = "hans";
.street = "xxx 13";
.city = "cologne";
}

( It is possible to use a pointer with a very short, undescriptive name, but
that makes the
code unreadable and stupid ( variables should have explanative names, not a
confusing x* )

Norman
Post by Erik Schnetter
The example
struct S
{
int x;
int y;
};
int main()
{
S s;
with (s)
{
.x = 1;
.y = 2;
}
return 0;
}
can easily be rewritten by introducing temporary references, as in
int main()
{
S s;
{
S& t = s;
t.x = 1;
t.y = 2;
}
return 0;
}
This requires only one additional variable reference each time the
"with" object is used. Additionally, it allows several "with" objects
(with different names) at the same time. In C, the same thing can be
done by using pointers.
-erik
Russ Allbery
2002-12-29 19:37:07 UTC
Permalink
Post by Norman Jonas
with ( verylongdescriptivename )
{
.name = "hans";
.street = "xxx 13";
.city = "cologne";
}
( It is possible to use a pointer with a very short, undescriptive name,
but that makes the code unreadable and stupid ( variables should have
explanative names, not a confusing x* )
I must admit that I don't see the effective difference between:

with (verylongdescriptivename)
{
.name = "hans";
.street = "xxx 13";
.city = "cologne";
}

and

{
struct S& s = verylongdescriptivename;
s.name = "hans";
s.street = "xxx 13";
s.city = "cologne";
}

You say that variables should have long, explanative names, but the
function of with is essentially to remove those names and rely on implicit
context. That's exactly what using short variable names does.

with is one of those constructs that renders code extremely difficult to
understand if used for blocks of code longer than a few lines, because it
complicates the way symbol lookups are performed (something that's already
incredibly complex in C++).
--
Russ Allbery (***@stanford.edu) <http://www.eyrie.org/~eagle/>
Loading...