WITH statement
Root Boy Jim
rbj at icst-cmr
Wed Jul 23 02:11:48 AEST 1986
> The ambiguous condition scares me too, and I think I could live
> much better with being able to take the address of the thing
> and then using it, like:
>
> recpointer := &recarray[selector];
> ...
> recpointer^.foo := 1;
> recpointer^.bar := 2;
I don't intend to flame, but there's a funny ring to this sort
of view of the (Pascal) `with' statement. C programmers
roundly criticize Pascal as being more cumbersome in notation.
I generally agree; Pascal could allow more terse expression
without suffering...but then why is it scary when Pascal has a
construct which allows more terse expression than C?
There's a fear down here we can't forget, hasn't got a name just yet.
Because the association is not explicit. I suppose if two records
had the same member name you would get a complaint, but changing
the names later would leave you no indication of what the code meant.
First off, the standard disclaimer: the `with' statement can
be abused, as can any statement (including the null statement!)
in any language.
I agree, I'm not picking this nit.
The `with' statement is an interesting construct from a
programming language standpoint;
I agree. It's a good idea, but a bad implementation. Saving two
chars isn't too Wirthwhile (moan), a better way might have been:
`with pointer^array[index]^ as p, quitelong[i] as q do ...
I don't know of too many
others in its class. One way to think of it is as a
procedure:
with p^, q[i] do...
is like starting a procedure right there with (value)
parameters being the records given. Within the procedure you
have access to these objects which is more convenient and
presumably more efficient. It's a funny open-code sort of
thing.
Opening a with using two records of the same type is possible,
but rare because it's kind of silly.
Why do you say that? Consider copying or comparing two records. And
how do the member names know which pointer to use? And if you use
different record types, what if the members have the same names?
Is this even legal?
(It has nowhere near the potential for screwup of, say, calling
a procedure with two pointer parameters pointing to the same object.)
That might be what you want to do.
The "scary" part of the `with' statement--the loss of qualifier
in front of field selectors--is no problem at all in practice
because `with' statements tend to be intensive activity on the
records being manipulated. I've observed that `with'
statements tend to be either fairly short (probably under 10
lines) or the entirety of a procedure where the `with' opens
one or more of the procedure's parameters--for example:
procedure xxx(p: pwhatzy);
...decls
begin with p^ do begin
...work on the object referenced by p
end end;
I agree here too. Almost anything is OK, as long as it is localized.
I've written a lot of Pascal and a lot more C. Mostly I find C
faster to write, but when I start writing a sequence of code to
fill in a structure, I sure miss the `with' statement. --
You have it, either symbolically via #define p pointer->array[index]
or by assigning pointer->array[index] to a (possibly register) variable p.
I guess the thing that bothers me most about Pascal is that it's a half
assed job, whipped up to teach a few good ideas, without giving thought
to completeness or applicability to complex tasks. Its author then
abandoned it and went on to design other languages which he also
then abandoned instead of fixing his mistakes.
Dick Dunn {hao,ucbvax,allegra}!nbires!rcd (303)444-5710 x3086
(Root Boy) Jim Cottrell <rbj at icst-cmr.arpa>
...At last it's the real thing...or close enough to pretend.
It's even worse than it appears...
More information about the Comp.lang.c
mailing list